【问题标题】:php imagecreatefrompng() not workingphp imagecreatefrompng()不起作用
【发布时间】:2011-06-28 21:53:17
【问题描述】:

我正在从另一个现有图像中裁剪图像。它适用于 jpg、jpeg、gif 正常工作。但它不适用于 png 图像文件。出现以下错误。

Warning (2): imagecreatefrompng() [function.imagecreatefrompng]: '/var/www/shareme/app/webroot//documents/users/MTI5NzkyMjQzMmZmLWxvZ28tYmlnLnBuZw.png' is not a valid PNG file [APP/controllers/components/jq_imgcrop.php, line 80]

这是代码。

function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
        $newImageWidth = ceil($width * $scale);
        $newImageHeight = ceil($height * $scale);

        $ext = strtolower(substr(basename($image), strrpos(basename($image), ".") + 1));
        $source = "";
        if($ext == "png"){
            $source = imagecreatefrompng($image);
        }elseif($ext == "jpg" || $ext == "jpeg"){
            $source = imagecreatefromjpeg($image);
        }elseif($ext == "gif"){
            $source = imagecreatefromgif($image);
        }
        $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);

        imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
        imagejpeg($newImage,$thumb_image_name,90);
        chmod($thumb_image_name, 0777);
        return $thumb_image_name;
    }

你对这个问题有什么想法吗?

【问题讨论】:

    标签: php image image-processing image-manipulation jcrop


    【解决方案1】:

    也许您还想检查 mime 类型而不是潜在的错误扩展。

    $handle = finfo_open(FILEINFO_MIME); 
    $mime_type = finfo_file($handle, $src);
    $mime_type = mime_content_type($src);
    switch(strtolower($mime_type)) {
        case 'image/gif':
            $img = imageCreateFromGIF($src);
            break;
        case 'image/png':
            $img = imageCreateFromPNG($src);
            break;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 2011-01-18
      • 2013-09-21
      • 1970-01-01
      • 2018-11-30
      • 2013-05-26
      • 1970-01-01
      相关资源
      最近更新 更多