无法在PHP中更改图像大小?

时间:2015-05-17 08:21:01

标签: php image resize jpeg scale

我有以下PHP代码......

$destination_image_x = "235";  
$destination_image_y = "230";  
$destination_image = imagecreatetruecolor($destination_image_x, $destination_image_y);  
$source_image_x = imagesx($temp_profile_picture_converted);  
$source_image_y = imagesy($temp_profile_picture_converted);  
$temp_profile_picture_converted = imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y);  

imagejpeg($temp_profile_picture_converted, $user_profile_picture_filename,'75');  
imagedestroy($temp_profile_picture_converted);  

此代码的功能是缩放传递给它的图像,并将其保存在指定的目录中。我可以使用" imagejpeg"来保存图像。通常如果我省略调整大小片段。变量" $ temp_profile_picture_converted"被分配到我使用" imagecreatefromjpeg从用户上传的图像创建的jpg图像。" (或imagecreatefrompng,或imagecreatefromgif等)

1 个答案:

答案 0 :(得分:1)

您在以下行中使用了两次相同的变量$temp_profile_picture_converted。函数imagecopyresampled()返回一个布尔值,并覆盖此变量保存的图像。此函数的返回值仅用于检查成功。将其更改为:

if (! imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y)){
    // then give error message...
}  

<强>更新 但是,您还有其他错误。您需要更改imagejpeg()的第一个参数。我还将大小变量从字符串更改为数字 - 不确定它是否重要。

imagejpeg($destination_image, $user_profile_picture_filename,75); 

我成功运行了以下代码

$destination_image_x = 235;  
$destination_image_y = 230;  

$source_image_x = imagesx($temp_profile_picture_converted);  
$source_image_y = imagesy($temp_profile_picture_converted);

$destination_image = imagecreatetruecolor($destination_image_x, $destination_image_y);  

imagecopyresampled($destination_image, $temp_profile_picture_converted, 0, 0, 0, 0, $destination_image_x, $destination_image_y, $source_image_x, $source_image_y);
imagejpeg($destination_image, $user_profile_picture_filename,75);  
imagedestroy($temp_profile_picture_converted);  
imagedestroy($destination_image);  

请注意,我还添加了最后一个语句imagedestroy($destination_image);

相关问题