php - 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;
}
提前感谢您的时间和帮助
解决方案
如果不是jpg,您将失败。请改用imagecreatefromstring()。这可以猜出真正的格式。
像这样替换代码
$img_src = imagecreatefromstring(file_get_contents($oldimage_name));
推荐阅读
- swift - RxTest - 如何比较事件
因为 Void 不是 Equatable - javascript - 路由器视图未列出组件
- android - 类 ContentResolver 或其超类中没有虚拟方法 loadThumbnail()
- c - C4496 和 C6387 用于使用 strcpy 和 strcat
- typescript - TypeScript 在通过键访问时丢失类型
- c - 使用 c 的 TCP/IP 服务器
- tensorflow - GPU工作时colab中的TPU失败
- python-3.x - 如何使用带有硒和python绑定的xpath仅提取不在任何标签内的文本
- php - 分析此日期时间结果并在 laravel 中仅导出日期
- windows - 批处理文件 - 在字符之前检测重复的文件名?