PHP - ImageCopyResampled 问题
这是我第一次使用 ImageCopyResampled 函数。我只是按照 PHP 手册中编写的代码进行操作。当我运行代码时,似乎没有错误。问题是我的代码基本上只是复制原始图像,并且没有遵循函数中传递的参数中定义的尺寸。下面是我的代码:
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;
}
这个函数只是复制源文件,没有发生裁剪。我错过了什么?
顺便说一句,我正在使用 kohana 3。谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有将
$trgt_img
保存到文件中,因此当脚本结束时,裁剪后的图像会丢失。您需要使用
imageJPEG()
(或任何格式)写出数据你想写信给)。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).Pekka的答案是正确的,但是保存的文件名不正确,应该是$trgt_file而不是$filename;
Pekka's answer is correct, but the filename that is being saved as is incorrect, should be $trgt_file instead of $filename;