【问题标题】:PHP imagecreatefromjpeg works, so why doesn't png/bmp/gif work?PHP imagecreatefromjpeg 有效,那么为什么 png/bmp/gif 无效?
【发布时间】:2012-04-19 09:13:42
【问题描述】:

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

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

大部分代码如下:

$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 文件则不能。我没有想法...
提前致谢!

【问题讨论】:

  • 你是如何填充$type的?似乎可能的答案是它总是2...
  • @DaveRandom getimagesize() 获取宽度、高度、类型(0-16)和一个属性,如果我没记错的话。类型并不总是 2,它会改变它应该如何。

标签: php image image-resizing


【解决方案1】:

您的 PHP 编译时可能不支持这些格式。运行 phpinfo() 并检查输出是否有类似这样的内容:

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

【讨论】:

  • 感谢您的帮助!有一个警告,我看不到,因为在处理后我将页面重定向回上传表单。原来我没有 libPNG,也不知道这些函数不是我的 PHP 库的一部分。现在我将下载它并尝试我的代码是否有效。
【解决方案2】:

由于您似乎每次都得到一个损坏的 33 字节图像文件,而不是 jpg,这可能是写入文件的 PHP 错误(因为您的 PHP 文件似乎直接显示图像内容,来自您的内容类型标题)。您是否尝试过在文本编辑器中打开文件并查看其内容?如果它是乱码,那么它就是一个错误,但如果它是一个 PHP 警告,那么您的 PHP 版本可能不支持这些扩展图像类型。那个,或者你的代码中可能有一个警告。

【讨论】:

  • 在文本编辑器中,jpeg 文件中的第一个单词是 CREATOR: gd-jpeg v1.0 (使用 IJG JPEG v62) 在 png、gif 和 bmp 中,有 PNG 或 GIF 之类的扩展名,和一些随机字符
  • 有一个警告,我看不到,因为在处理后我将页面重定向回上传表单。原来我没有 libPNG,也不知道这些函数不是我的 PHP 库的一部分。现在我将下载它并尝试我的代码是否有效。感谢您的帮助!
  • 我得到了相同的 33 字节 png 文件。在文本编辑器中,我看到:“PNG Image: ‘file.png’ pixelWidth: pixelHeight: ” phpinfo 表明我已正确启用所有内容。有什么想法吗?
  • 刚刚解决了:imagepng不接受80作为压缩,需要0-9
猜你喜欢
  • 2016-12-15
  • 2010-09-29
  • 1970-01-01
  • 2010-12-16
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 2020-04-03
相关资源
最近更新 更多