Scale picture and cut picture code sharing with PHP
/** * Picture scaling function (can be set height fixed, width fixed or maximum width, support gif/jpg/png three types) * author:specs * * @param string $source _path source picture * @param int $target _width Target Width * @param int $target _height Target Height * @param string $fixed _orig lock width (optional parameter width, height, or null value) * @return s Tring/function Myimageresize ($source _path, $target _width =, $target _height =, $fixed _orig = ') {$source _inf
o = getimagesize ($source _path);
$source _width = $source _info[0];
$source _height = $source _info[1];
$source _mime = $source _info[' mime ');
$ratio _orig = $source _width/$source _height;
if ($fixed _orig = = ' width ') {//width fixed $target _height = $target _width/$ratio _orig;
}elseif ($fixed _orig = = ' height ') {//height fixed $target _width = $target _height * $ratio _orig; }else{//Max width or maximum high if ($target _width/$target _height > $ratio _orig) {$target _width = $target _height * $rat
Io_orig;
}else{$target _height = $target _width/$ratio _orig;
Switch ($source _mime) {case ' image/gif ': $source _image = imagecreatefromgif ($source _path);
Break
Case ' image/jpeg ': $source _image = imagecreatefromjpeg ($source _path);
Break
Case ' image/png ': $source _image = imagecreatefrompng ($source _path);
Break
Default:return false;
Break
} $target _image = Imagecreatetruecolor ($target _width, $target _height); Imagecopyresampled ($target _image, $source _image, 0, 0, 0, 0, $target _width, $target _height, $source _width, $source _
height);
Header (' Content-type:image/jpeg ');
$IMGARR = Explode ('. ', $source _path); $target _path = $imgArr [0]. ' _new. '
$IMGARR [1];
Imagejpeg ($target _image, $target _path, 100);
}
Usage:
- Myimageresize ($filename, 200, 200); Max width and height
- Myimageresize ($filename, M, ' width '); Width fixed
- Myimageresize ($filename, M, ' height '); Highly fixed
Cut picture as fixed size:
function Imagecropper ($source _path, $target _width, $target _height) {$source _info = getimagesize ($source _path);
$source _width = $source _info[0];
$source _height = $source _info[1];
$source _mime = $source _info[' mime ');
$source _ratio = $source _height/$source _width;
$target _ratio = $target _height/$target _width;
Source graph is too high if ($source _ratio > $target _ratio) {$cropped _width = $source _width;
$cropped _height = $source _width * $target _ratio;
$source _x = 0;
$source _y = ($source _height-$cropped _height)/2;
}elseif ($source _ratio < $target _ratio) {//source graph is too wide $cropped _width = $source _height/$target _ratio;
$cropped _height = $source _height;
$source _x = ($source _width-$cropped _width)/2;
$source _y = 0;
}else{//source map Moderate $cropped _width = $source _width;
$cropped _height = $source _height;
$source _x = 0;
$source _y = 0;
Switch ($source _mime) {case ' image/gif ': $source _image = imagecreatefromgif ($source _path); Break
Case ' image/jpeg ': $source _image = imagecreatefromjpeg ($source _path);
Break
Case ' image/png ': $source _image = imagecreatefrompng ($source _path);
Break
Default:return false;
Break
} $target _image = Imagecreatetruecolor ($target _width, $target _height);
$cropped _image = Imagecreatetruecolor ($cropped _width, $cropped _height);
Crop Imagecopy ($cropped _image, $source _image, 0, 0, $source _x, $source _y, $cropped _width, $cropped _height); Zoom imagecopyresampled ($target _image, $cropped _image, 0, 0, 0, 0, $target _width, $target _height, $cropped _width, $crop
Ped_height);
$dotpos = Strrpos ($source _path, '. ');
$imgName = substr ($source _path, 0, $dotpos);
$suffix = substr ($source _path, $dotpos); $imgNew = $imgName. ' _small '.
$suffix;
Imagejpeg ($target _image, $imgNew, 100);
Imagedestroy ($source _image);
Imagedestroy ($target _image);
Imagedestroy ($cropped _image);
}