PHP imagecreatefromjpeg有效,为什么png / bmp / gif没有工作?

编程入门 行业动态 更新时间:2024-10-27 21:13:17
本文介绍了PHP imagecreatefromjpeg有效,为什么png / bmp / gif没有工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想上传和调整不同扩展名的图片。 php从原始图片的中心裁剪出最大可能的正方形,然后将其保存为360 * 360像素。

I want to upload and resize pictures with different extensions. The php crops the biggest possible square from the center of the original pic, then saves it in 360*360 pixels.

代码适用于jpeg文件,但是使用gif,bmp和png我得到一个33字节大小的损坏文件。

The code works fine with jpeg files, but with gif, bmp and png I get a corrupted file with 33 Byte size.

以下是大部分代码:

$file_temp = $_FILES["pic"]["tmp_name"]; list ($width, $height, $type) = getimagesize ($file_temp); $picture_name = "... a name.ext ..."; $upload = "... some dir/$picture_name"; if (move_uploaded_file($file_temp, "$upload")) { //switches content-type and calls the imagecreatefrom... function if ($type == 1) { header ('Content-Type: image/gif'); $image = imagecreatefromgif($upload); } elseif ($type == 2) { header ('Content-Type: image/jpeg'); $image = imagecreatefromjpeg($upload); } elseif ($type == 3) { header ('Content-Type: image/png'); $image = imagecreatefrompng($upload); } else { header ('Content-Type: image/x-ms-bmp'); $image = imagecreatefromwbmp($upload); } $image_p = imagecreatetruecolor(360, 360); //this code below should preserve transparency but I couldn't try it out for now... if($type==1 or $type==3) { imagecolortransparent($image_p, imagecolorallocatealpha($image_p, 0, 0, 0, 127)); imagealphablending($image_p, true); imagesavealpha($image_p, true); } //this part is for cropping $x=0; $y=0; if ($width > $height) { $x= ($width - $height)/2; $width = $height; } else { $y = ($height - $width)/2; $height = $width; } imagecopyresampled ($image_p, $image, 0, 0, $x, $y, 360, 360, $width, $height); if ($type == 1) imagegif ($image_p, $upload, 80); elseif ($type == 2) imagejpeg ($image_p, $upload, 80); elseif ($type == 3) imagepng ($image_p, $upload, 80); else imagewbmp ($image_p, $upload, 80); }

因此,只有正确处理jpeg文件,gif,png和bmp文件都没有T。我没有想法... 提前致谢!

So, only jpeg files are processed correctly, gif, png and bmp files aren't. I'm out of ideas... Thanks in advance!

推荐答案

您的PHP可能不是编译时支持这些格式。运行phpinfo()并检查输出,如下所示:

Your PHP may not be compiled with support for those formats. Run a phpinfo() and inspect the output for something like this:

GD Support => enabled GD Version => bundled (2.0.34 compatible) GIF Read Support => enabled GIF Create Support => enabled PNG Support => enabled libPNG Version => 1.2.10

更多推荐

PHP imagecreatefromjpeg有效,为什么png / bmp / gif没有工作?

本文发布于:2023-10-16 22:12:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   imagecreatefromjpeg   PHP   png   gif

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!