-
-
function CreateImage($SrcImageUrl, $DirImageUrl, $Width, $Height)
- {
- $img;
- $srcw;
- $new_width;
- $srch;
- $new_height;
- // 图片类型
- $type = substr(strrchr($SrcImageUrl, "."), 1);
- // 初始化图像
- if($type == "jpg")
- $img = imagecreatefromjpeg($SrcImageUrl);
- if($type == "gif")
- $img = imagecreatefromgif($SrcImageUrl);
- if($type == "png")
- $img = imagecreatefrompng($SrcImageUrl);
$srcw = imagesx($img);
- $srch = imagesy($img);
if ($srcw / $srch > $Width / $Height)
- {
- if ($srcw > $Width)
- {
- $new_width = $Width;
- $new_height = $srch * ($Width / $srcw);
- }
- else
- {
- $new_width = $srcw;
- $new_height = $srch;
- }
- }
- else
- {
- if ($srch > $Height)
- {
- $new_height = $Height;
- $new_width = $srcw * ($Height / $srch);
- }
- else
- {
- $new_width = $srcw;
- $new_height = $srch;
- }
- }
$new_image = imagecreatetruecolor($new_width, $new_height);
- imagecopyresampled($new_image, $img, 0, 0, 0, 0, $new_width, $new_height, $srcw, $srch);
- imagejpeg($new_image, $DirImageUrl);
imagedestroy($img);
- imagedestroy($new_image);
- }
-
复制代码
3. Improved code:
-
-
- /*
*$o_photo Original image path
*$d_photo Processed image Path
*$width Define width
*$height Define height
*Call method cutphoto("test.jpg ","temp.jpg",256,146);
*/
function cutphoto($o_photo,$d_photo,$width,$height){< ;/p>
$temp_img = imagecreatefromjpeg($o_photo);
$o_width = imagesx($temp_img); //Get the original image width
- < ;p>$o_height = imagesy($temp_img); //Get the original image height
//Judge the processing method
if($width>$o_width || $height>$o_height){//The width or height of the original image is smaller than the specified size, and is compressed
$newwidth=$o_width;
$newheight=$o_height;
if($o_width>$width){
$newwidth=$width;
}
if($newheight>$height){
- < ;p>$newwidth=$newwidth*$height/$newheight;
$newheight=$height;
}
- < p>//Thumbnail image
$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{//The original image width and height are larger than the specified size, compress and then crop
if($o_height*$width /$o_width>$height){//First make sure the width is the same as the specification. If the height is larger than the specification, then ok
$newwidth=$width;
$newheight=$o_height*$width/$o_width;
$x=0;
$y=($newheight-$height)/2;
}else{ // Otherwise, make sure the height is the same as specified, and the width is adaptive
$newwidth=$o_width*$height/$o_height; p>
$newheight=$height;
$x=($newwidth-$width)/2;
$y=0 ;
}
//Thumbnail image
$new_img = imagecreatetruecolor($newwidth, $newheight);< ;/p>
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);
- < p>$o_width = imagesx($temp_img); //Get the thumbnail width
$o_height = imagesy($temp_img); //Get the thumbnail height
- < p>//Crop the image
$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);
- }
- }
- ?>
-
Copy code
|