The following is the source code:
<?php
/*
* $o _photo Original path
* $d _photo image path after processing
* $width definition width
* $height High Definition
* Call Method Cutphoto ("Test.jpg", "temp.jpg", 256,146);
*/
function Cutphoto ($o _photo, $d _photo, $width, $height) {
$temp _img = imagecreatefromjpeg ($o _photo);
$o _width = imagesx ($temp _img); Get the original image wide
$o _height = Imagesy ($temp _img); Get high
Judgment Processing method
if ($width > $o _width | | $height > $o _height) {//Huantuquan or higher than the specified size, compress
$newwidth = $o _width;
$newheight = $o _height;
if ($o _width> $width) {
$newwidth = $width;
$newheight = $o _height* $width/$o _width;
}
if ($newheight > $height) {
$newwidth = $newwidth * $height/$newheight;
$newheight = $height;
}
Thumbnail pictures
$new _img = Imagecreatetruecolor ($newwidth, $newheight);
Imagecopyresampled ($new _img, $temp _img, 0, 0, 0, 0, $newwidth, $newheight, $o _width, $o _height);
Imagejpeg ($new _img, $d _photo);
Imagedestroy ($new _img);
}else{//Huantuquan and height are larger than the specified size, after compression cut
if ($o _height* $width/$o _width> $height) {//Make sure the width is the same as the rule, if the height is larger than the specified, OK
$newwidth = $width;
$newheight = $o _height* $width/$o _width;
$x = 0;
$y = ($newheight-$height)/2;
}else{//Otherwise determine the height is the same as the specified, Width adaptive
$newwidth = $o _width* $height/$o _height;
$newheight = $height;
$x = ($newwidth-$width)/2;
$y = 0;
}
Thumbnail pictures
$new _img = Imagecreatetruecolor ($newwidth, $newheight);
Imagecopyresampled ($new _img, $temp _img, 0, 0, 0, 0, $newwidth, $newheight, $o _width, $o _height);
Imagejpeg ($new _img, $d _photo);
Imagedestroy ($new _img);
$temp _img = imagecreatefromjpeg ($d _photo);
$o _width = imagesx ($temp _img); Get thumbnail width
$o _height = Imagesy ($temp _img); Get thumbnail height
Crop a picture
$new _IMGX = Imagecreatetruecolor ($width, $height);
Imagecopyresampled ($new _imgx, $temp _img,0,0, $x, $y, $width, $height, $width, $height);
Imagejpeg ($new _imgx, $d _photo);
Imagedestroy ($new _IMGX);
}
}
?> |