Common PHP Image processing classes (watermarks, equal scaling, fixed width) sharing
<?php//php Add watermark & Proportional thumbnails & fixed height & fixed width class. Class image_process{Public $source//original artwork public $source _width; Original width public $source _height;
Original height public $source _type_id;
Public $orign _name;
Public $orign _dirname;
Incoming artwork path public function __construct ($source) {$this->typelist = array (1=> ' gif ',2=> ' jpg ',3=> ' png ');
$ginfo = getimagesize ($source);
$this->source_width = $ginfo [0];
$this->source_height = $ginfo [1];
$this->source_type_id = $ginfo [2];
$this->orign_url = $source;
$this->orign_name = basename ($source);
$this->orign_dirname = dirname ($source); }//Judge the format of the file of the picture, return the encoded public function Judgetype ($type, $source) of the PHP can be identified {if ($type = 1) {returns IMAGECR Eatefromgif ($source); GIF}else if ($type = = 2) {return imagecreatefromjpeg ($source);//jpg}else if ($type = = 3) {retur n Imagecreatefrompng ($source); PNG}else{RetuRN false;
///Generate Watermark Picture public function Watermakeimage ($logo) {$linfo = getimagesize ($logo);
$logo _width = $linfo [0];
$logo _height = $linfo [1];
$logo _type_id = $linfo [2];
$sourceHandle = $this->judgetype ($this->source_type_id, $this->orign_url);
$logoHandle = $this->judgetype ($logo _type_id, $logo);
if (! $sourceHandle | |! $logoHandle) {return false;
$x = ($this->source_width-$logo _width)/2;
$y = ($this->source_height-$logo _height)/2;
Imagecopy ($sourceHandle, $logoHandle, $x, $y, 0,0, $logo _width, $logo _height); $newPic = $this->orign_dirname. ' \water_ '. Time (). '. '.
$this->typelist[$this->source_type_id];
if ($this->saveimage ($sourceHandle, $newPic)) {Imagedestroy ($sourceHandle);
Imagedestroy ($logoHandle); }//fixed height width public function fixsizeimage ($width, $height) {if ($width > $this->source_width) $this
->source_width; if ($heighT > $this->source_height) $this->source_height;
if ($width = = False) {$width = Floor ($this->source_width/($this->source_height/$height));
} if ($height = = False) {$height = Floor ($this->source_height/($this->source_width/$width));
$this->tinyimage ($width, $height);
//Proportional Scaling picture public function scaleimage ($scale) {$width = Floor ($this->source_width * $scale);
$height = Floor ($this->source_height * $scale);
$this->tinyimage ($width, $height);
//Create thumbnail public function tinyimage ($width, $height) {$tinyImage = Imagecreatetruecolor ($width, $height);
$handle = $this->judgetype ($this->source_type_id, $this->orign_url); if (function_exists (' imagecopyresampled ')) {imagecopyresampled ($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $th
Is->source_width, $this->source_height); }else{imagecopyresized ($tinyImage, $handle, 0, 0, 0, 0, $width, $height, $this->source_width, $this->source_height); $newPic = $this->orign_dirname. ' \thumb_ '. Time (). ' _ '. $width. " _ ". $height.".
$this->typelist[$this->source_type_id];
if ($this->saveimage ($tinyImage, $newPic)) {Imagedestroy ($tinyImage);
Imagedestroy ($handle);
}///Save picture Private Function SaveImage ($image, $url) {if (imagejpeg ($image, $url)) {return true;
}} $imgHandle = new image_process (' D:\AppServ\www\test\getimg\14061907445601.jpg '); $imgHandle->watermakeimage (' D:\AppServ\www\test\getimg\shougongke.png '); Generate watermark Picture//$imgHandle->fixsizeimage (200,150); Fixed length picture $imgHandle->scaleimage (0.2);
Equal scale scaling?>
Example two:
<?php/** * * Image processing Class * @author Fc_lamp * @internal features include: watermark, Thumbnail/class IMG {//Picture format Private $exts = array (' jpg
', ' jpeg ', ' gif ', ' BMP ', ' PNG '); /** * * * @throws Exception/Public Function __construct () {if (! function_exists (' Gd_info ')) {throw NE W Exception (' Loading GD library failed!
' ); }/** * * * @param $src _img picture * @param $save _img the generated picture * @param $option parameter options, including: $maxwidth width $maxheight
High * Array (' width ' =>xx, ' height ' =>xxx) * @internal * Our general compression image method, the picture is too long or too wide to generate the picture * will be "compressed", for this should be used first cut after the proportional compression method * * Public Function thumb_img ($src _img, $save _img = ", $option) {if (Empty ($option [' width ']) or empty ($option [' He
Ight ']) {return array (' flag ' => False, ' msg ' => ' original image length and width cannot be less than 0 ');
$org _ext = $this->is_img ($src _img);
if (! $org _ext [' flag ']) {return $org _ext;
//If there is a save path, determine if the path is correct if (! Empty ($save _img)) {$f = $this->check_dir ($save _img);
if (! $f [' flag ']) {return $f; }//Get the correspondingThe method $org _funcs = $this->get_img_funcs ($org _ext [' msg ']);
Gets the original size $source = $org _funcs [' Create_func '] ($src _img);
$src _w = Imagesx ($source);
$src _h = Imagesy ($source); Adjust the original image (keep the picture original shape cropped image) $dst _scale = $option [' height ']/$option [' width ']; The aspect ratio of target image $src _scale = $src _h/$src _w;
The original aspect ratio if ($src _scale >= $dst _scale) {//too high $w = Intval ($src _w);
$h = intval ($dst _scale * $w);
$x = 0;
$y = ($src _h-$h)/3;
else {//too wide $h = intval ($src _h);
$w = Intval ($h/$DST _scale);
$x = ($src _w-$w)/2;
$y = 0;
}//Trim $croped = Imagecreatetruecolor ($w, $h);
Imagecopy ($croped, $source, 0, 0, $x, $y, $src _w, $src _h);
Zoom $scale = $option [' width ']/$w;
$target = Imagecreatetruecolor ($option [' width '], $option [' height ']);
$final _w = intval ($w * $scale);
$final _h = intval ($h * $scale);
Imagecopyresampled ($target, $croped, 0, 0, 0, 0, $final _w, $final _h, $w, $h);
Imagedestroy ($croped);
Output (Save) pictureif (! empty ($save _img)) {$org _funcs [' Save_func '] ($target, $save _img);
else {header ($org _funcs [' header ']);
$org _funcs [' Save_func '] ($target);
} Imagedestroy ($target);
Return Array (' flag ' => True, ' msg ' => '); /** * * Proportional scaling image * @param $src _img Original picture * @param $save where _img needs to be saved * @param $option parameters set Array (' Width ' =>xx, ' hei Ght ' =>xxx) */function Resize_image ($src _img, $save _img = ', $option) {$org _ext = $this->is_img ($src _img
);
if (! $org _ext [' flag ']) {return $org _ext;
//If there is a save path, determine if the path is correct if (! Empty ($save _img)) {$f = $this->check_dir ($save _img);
if (! $f [' flag ']) {return $f;
}//Get the appropriate method $org _funcs = $this->get_img_funcs ($org _ext [' msg ']);
Gets the original size $source = $org _funcs [' Create_func '] ($src _img);
$src _w = Imagesx ($source);
$src _h = Imagesy ($source); if ($option [' width '] && $src _w > $option [' width ']) | | ($option [' height '] && $src _h > $option [' height ']) {if ($option [' width '] && $src _w > $option [' width ']) {$widthratio = $option [' W]
Idth ']/$src _w;
$resizewidth _tag = true;
} if ($option [' height '] && $src _h > $option [' height ']) {$heightratio = $option [' height ']/$src _h;
$resizeheight _tag = true;
} if ($resizewidth _tag && $resizeheight _tag) {if ($widthratio < $heightratio) $ratio = $widthratio;
else $ratio = $heightratio;
if ($resizewidth _tag &&! $resizeheight _tag) $ratio = $widthratio;
if ($resizeheight _tag &&! $resizewidth _tag) $ratio = $heightratio;
$newwidth = $src _w * $ratio;
$newheight = $src _h * $ratio;
if (function_exists ("imagecopyresampled")) {$newim = Imagecreatetruecolor ($newwidth, $newheight);
Imagecopyresampled ($newim, $source, 0, 0, 0, 0, $newwidth, $newheight, $src _w, $src _h);
else {$newim = Imagecreate ($newwidth, $newheight); Imagecopyresized ($newim, $Source, 0, 0, 0, 0, $newwidth, $newheight, $src _w, $src _h);
//output (save) Picture if (! empty ($save _img)) {$org _funcs [' Save_func '] ($newim, $save _img);
else {header ($org _funcs [' header ']);
$org _funcs [' Save_func '] ($newim);
} Imagedestroy ($newim);
Return Array (' flag ' => True, ' msg ' => '); /** * * Generate watermark Picture * @param $org _img Original image * @param $mark _img watermark Tag Image * @param $save _img When its directory does not exist, try to create a directory * @param arr Ay $option some basic settings for watermarks include: * x: Watermark horizontal position, default to minus watermark width of the value * Y: Watermark vertical position, the default is minus the watermark height after the value * Alpha:alpha value (control transparency), the default is * * * Public FU Nction Water_mark ($org _img, $mark _img, $save _img = ", $option = Array ()) {//check picture $org _ext = $this->is_img ($org _i
MG);
if (! $org _ext [' flag ']) {return $org _ext;
$mark _ext = $this->is_img ($mark _img);
if (! $mark _ext [' flag ']) {return $mark _ext;
//If there is a save path, determine if the path is correct if (! Empty ($save _img)) {$f = $this->check_dir ($save _img);
if (! $f [' flag ']) {return $f; }//Get the appropriate canvas
$org _funcs = $this->get_img_funcs ($org _ext [' msg ']);
$org _img_im = $org _funcs [' Create_func '] ($org _img);
$mark _funcs = $this->get_img_funcs ($mark _ext [' msg ']);
$mark _img_im = $mark _funcs [' Create_func '] ($mark _img);
Copy watermark image coordinates $mark _img_im_x = 0;
$mark _img_im_y = 0;
Copy watermark Picture High width $mark _img_w = imagesx ($mark _img_im);
$mark _img_h = Imagesy ($mark _img_im);
$org _img_w = imagesx ($org _img_im);
$org _img_h = imagesx ($org _img_im);
The synthetic point coordinates $x = $org _img_w-$mark _img_w; $org _img_im_x = isset ($option [' x '])?
$option [' x ']: $x; $org _img_im_x = ($org _img_im_x > $org _img_w or $org _img_im_x < 0)?
$x: $org _img_im_x;
$y = $org _img_h-$mark _img_h; $org _img_im_y = isset ($option [' Y '])?
$option [' y ']: $y; $org _img_im_y = ($org _img_im_y > $org _img_h or $org _img_im_y < 0)?
$y: $org _img_im_y; Alpha $alpha = isset ($option [' alpha '])?
$option [' alpha ']: 50; $alpha = ($alpha > or $alpha < 0)?
: $alpha; Merging diagramsFilm Imagecopymerge ($org _img_im, $mark _img_im, $org _img_im_x, $org _img_im_y, $mark _img_im_x, $mark _img_im_y, $mark _img_w
, $mark _img_h, $alpha);
Output (save) Picture if (! empty ($save _img)) {$org _funcs [' Save_func '] ($org _img_im, $save _img);
else {header ($org _funcs [' header ']);
$org _funcs [' Save_func '] ($org _img_im);
//Destroy Canvas Imagedestroy ($org _img_im);
Imagedestroy ($mark _img_im);
Return Array (' flag ' => True, ' msg ' => '); /** * * Check picture * @param unknown_type $img _path * @return Array (' flag ' =>true/false, ' msg ' =>ext/error message) * * * PRIV Ate function is_img ($img _path) {if (! file_exists ($img _path)) {return array (' flag ' => False, ' msg ' => ' loading Picture $img _path failed!
" );
$ext = Explode ('. ', $img _path);
$ext = Strtolower (end ($ext)); if (! In_array ($ext, $this->exts)) {return array (' flag ' => False, ' msg ' => ' picture $img _path is not in the correct format!)
" );
Return Array (' flag ' => True, ' msg ' => $ext); }/** * * returns correctThe picture function * @param unknown_type $ext/Private Function Get_img_funcs ($ext) {//select switch ($ext) {case ' jpg ': $h
Eader = ' content-type:image/jpeg ';
$createfunc = ' imagecreatefromjpeg ';
$savefunc = ' imagejpeg ';
Break
Case ' jpeg ': $header = ' content-type:image/jpeg ';
$createfunc = ' imagecreatefromjpeg ';
$savefunc = ' imagejpeg ';
Break
Case ' gif ': $header = ' content-type:image/gif ';
$createfunc = ' imagecreatefromgif ';
$savefunc = ' imagegif ';
Break
Case ' BMP ': $header = ' content-type:image/bmp ';
$createfunc = ' imagecreatefrombmp ';
$savefunc = ' imagebmp ';
Break
Default: $header = ' content-type:image/png ';
$createfunc = ' imagecreatefrompng ';
$savefunc = ' imagepng ';
Return Array (' Save_func ' => $savefunc, ' Create_func ' => $createfunc, ' header ' => $header);
/** * * Check and try to create directory * @param $save _img/Private Function Check_dir ($save _img) {$dir = DirName ($save _img); if (! Is_dir ($dir)) {if (! mkdir ($dir, 0777, True)} {return Array (' flag ' => False, ' msg ' => ' picture save directory $dir cannot be created!
" );
} return Array (' flag ' => True, ' msg ' => ');
} if (! empty ($_files [' Test '] [' tmp_name '])) {//Example $IMG = new img ();
Artwork $name = explode ('. ', $_files [' Test '] [' name ']); $org _img = ' d:/test. '
End ($name);
Move_uploaded_file ($_files [' Test '] [' tmp_name '], $org _img);
$option = Array (' width ' => $_post [' width '], ' height ' => $_post [' height ']);
if ($_post [' type '] = = 1) {$s = $img->resize_image ($org _img, ", $option);
else {$img->thumb_img ($org _img, ', $option);
} unlink ($org _img);
}
The above is the entire contents of this article, I hope you can enjoy.