I want to reduce a picture size from 600px * 500px to 60px * 50px size, then crop it become 50px *开发者_开发问答50px. I have two groups of codes, 1 is to reduce the size of image, other 1 is to crop the image. The problem is they works separately, how to combine this two groups of codes to make them work together? Below is my codes :
<?php
//codes of group A - Reduce the size of image from 600px * 500px to 60px * 50px
$save2 = "images/users/" . $image_name_2; //This is the new file you saving
list($width2, $height2) = getimagesize($file) ;
$modwidth2 = 50;
$diff2 = $width2 / $modwidth2;
$modheight2 = $height2 / $diff2;
$tn2 = imagecreatetruecolor($modwidth2, $modheight2) ;
$image2 = imagecreatefromjpeg($file) ;
imagecopyresampled($tn2, $image2, 0, 0, 0, 0, $modwidth2, $modheight2, $width2, $height2) ;
imagejpeg($tn2, $save2, 100) ;
//codes of group B - Crop the image from 60px * 50px to 50px * 50px
$save3 = "images/users/" . $image_name_3;
list($width3, $height3) = getimagesize($file) ;
$modwidth3 = 60;
$diff3 = $width3 / $modwidth3;
$modheight3 = $height3 / $diff3;
$left = 0;
$top = 0;
$cropwidth = 50; //thumb size
$cropheight = 50;
$tn3 = imagecreatetruecolor($cropwidth, $cropheight) ;
$image3 = imagecreatefromjpeg($file) ;
imagecopyresampled($tn3, $image3, 0, 0, $left, $top, $cropwidth, $cropheight, $modwidth3, $modheight3) ;
imagejpeg($tn3, $save3, 100) ; //save the cropped image
?>
As you can see from 2 groups of codes above, 1st group resize the pic then save it to a folder. 2nd group of codes crop the pic then save it into the folder too. My question is ... After 1st group of codes resize the picture, is it necessary to save it into folder before I can crop it? If it is necessary, then I need to write new lines of codes to retrieve the resized pic from the folder for 2nd group of codes to crop it? If it is not necessary, after resizing the pic, how do I pass the pic to 2nd group of codes to crop it?
Here you are @zac1987.
Full PHP code generating square thumbnail of desired size without stretching image. The code supports both png and jpg/jpeg image extensions. Simply change settings part to desired one. You can copy paste the code and test it on your web server.
<?php
// SETTINGS
$image_name = 'file.jpg'; // Full path and image name with extension
$thumb_name = 'thumbnail'; // Generated thumbnail name without extension
$thumb_side = 100; // Desired thumbnail side size
// END OF SETTINGS
$image_extension = explode('.', $image_name); // I assume that images are named only following 'imagename.ext' pattern
if (preg_match('/jpg|jpeg/', $image_extension[1])) {
$src_image = imagecreatefromjpeg($image_name);
$image_extension = 'jpg';
} else if (preg_match('/png/', $image_extension[1])) {
$src_image = imagecreatefrompng($image_name);
$image_extension = 'png';
}
$src_width = imageSX($src_image); // Width of the original image
$src_height = imageSY($src_image); // Height of the original image
$min_side = min($src_width, $src_height);
/*********** If you need this part uncomment it
$ratio = $min_side / $thumb_width;
$new_width = floor($src_width / $ratio);
$new_height = floor($src_height / $ratio);
**********************************************/
$dst_image = imagecreatetruecolor($thumb_side, $thumb_side);
imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $thumb_side, $thumb_side, $min_side, $min_side);
switch ($image_extension)
{
case 'jpg':
imagejpeg($dst_image, $thumb_name . '.jpg', 100);
break;
case 'png':
imagepng($dst_image, $thumb_name . '.jpg', 100);
break;
}
imagedestroy($src_image);
imagedestroy($dst_image);
?>
$modwidth3 = 500; //I resize the picture width to smaller as 60px.
$diff3 = $width3 / $modwidth3;
$modheight3 = $height3 / $diff3; //I resize the picture height to smaller.
$left = 1; //getting the left and top coordinate
$top = 1;
$cropwidth = 50; //thumb size
$cropheight = 50;
imagecopyresampled($tn3, $image3, 0, 0, $left, $top, $cropwidth, $cropheight, $modwidth3, $modheight3) ;
$modwidth3
and $modheight3
need to correspond to the width and height of the image you want to crop. Also $top
and $left
should match the top left coordinate of the area you want to crop. If the original image is 600x500 px and you want to resize and crop to 50x50, you should set $top to 0 and $left to 50 (px). Both $modwidth and $modheight should be set to 500. That keeps the full height of the original image and cuts away 50px from the left hand side and 50px from the right hand side. The remaining 500px is cropped to 50px.
link to a blog article i wrote about how to resize any size image to any arbitrary size. includes options for letterboxing and crop-to-fit.
http://www.spotlesswebdesign.com/blog.php?id=1
精彩评论