问题
It's my first time using the function ImageCopyResampled. I just followed the code written in the PHP manual. There seemed to be no errors when I ran the code. The problem is my code just basically copies the original image and did not follow the dimensions as it was defined in the parameters passed in the function. Below is my code:
public static function uploadFile($filename, $x_dimension, $y_dimension, $width, $height){
$file = DOCROOT . "uploads/temp/".$filename;
$trgt_file = DOCROOT . "uploads/images/thumbs/".$filename;
if(is_file($file) AND file_exists($file)):
$trgt_w = 198;
$trgt_h = 130;
if(copy($file, $trgt_file)):
$src_img = imageCreateFromJpeg($file);
$trgt_img = imageCreateTrueColor($trgt_w, $trgt_h);
imageCopyResampled($trgt_img, $src_img, 0, 0, $x_dimension, $y_dimension, $trgt_w, $trgt_h, $width ,$height);
unlink($file);
endif;
endif;
}
This function just copy the source file and no cropping happened. What did I miss?
BTW, Im using kohana 3. Thanks.
回答1:
You are not saving $trgt_img
to a file, so the cropped image gets lost when the script ends.
You need to write out the data using imageJPEG() (or whatever format you want to write to).
imageCopyResampled($trgt_img, $src_img, 0, 0,
$x_dimension, $y_dimension, $trgt_w, $trgt_h,
$width ,$height);
imagejpeg($trgt_img, $filename, 90); // 90 is for quality - 75 is the default
回答2:
Pekka's answer is correct, but the filename that is being saved as is incorrect, should be $trgt_file instead of $filename;
来源:https://stackoverflow.com/questions/2274266/php-problem-with-imagecopyresampled