首页 > 解决方案 > GD LIB如何将每张上传的图片成功转换为jpg类型?

问题描述

请帮助我使用我当前的功能,使其成功地将任何其他图像类型转换为正确的 jpg 图像类型。

这是我的功能

    function resize_image($oldimage_name, $new_image_name){
    list($owidth,$oheight) = getimagesize($oldimage_name);
    $width = 250; $height = 250;    
    $im = imagecreatetruecolor($width, $height);
    $img_src = imagecreatefromjpeg($oldimage_name);
    imagealphablending($img_src, false);
    imagesavelalpha($img_src, true);
    imagecopyresampled($im, $img_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
    imagejpeg($im, $new_image_name, 90);
    imagedestroy($im);
    unlink($oldimage_name);
    return true;
}

提前感谢您的时间和帮助

标签: phpfunctionimage-resizinggdlib

解决方案


如果不是jpg,您将失败。请改用imagecreatefromstring()。这可以猜出真正的格式。

像这样替换代码

$img_src = imagecreatefromstring(file_get_contents($oldimage_name));

推荐阅读