【问题标题】:Failing in creating thumbs for all mysql post img attachments未能为所有 mysql 帖子图像附件创建拇指
【发布时间】:2013-08-04 19:10:13
【问题描述】:

我正在尝试在我的网站中保存所有附加图像的副本。

加载时间太长了,所以我认为生成缩略图以显示在列表页面中并不是一个坏主意,

这就是我正在尝试的方式:

 <?php
    include('basedatos.php');
    class ImgResizer {
        var $originalFile = '$newName';
        function ImgResizer($originalFile = '$newName') {
            $this -> originalFile = $originalFile;
        }
        function resize($newWidth, $targetFile) {

            if (empty($newWidth) || empty($targetFile)) {
                return false;
            }
            $src = imagecreatefromjpeg($this -> originalFile);
            list($width, $height) = getimagesize($this -> originalFile);
            $newHeight = ($height / $width) * $newWidth;
            $tmp = imagecreatetruecolor($newWidth, $newHeight);
            imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
            if (exif_imagetype($tmp) != IMAGETYPE_JPEG) {
                imagecreatefromjpeg($tmp, $targetFile, 95);
            }else if(exif_imagetype($tmp) != IMAGETYPE_PNG){
                imagecreatefrompng($tmp, $targetFile, 95);
            }else if(exif_imagetype($tmp) != IMAGETYPE_GIF){
                imagecreatefromgif($tmp, $targetFile, 95);
            }else die('invalid image format');

        }

    }
    $query = "SELECT * FROM media where iframe <> 1";
    $mediafiles = mysql_query($query);
    while($details = mysql_fetch_array($mediafiles))
    {   // Copy each file from its temp directory to $ROOT

        $temp = '/home/deia1/public_html/'.$details['url'];
        $path = '/home/deia1/public_html/files/uploads/thumbs/'.str_replace('files/uploads/','',$details['url']);

        echo "$temp<br>";
        if(is_dir('/home/deia1/public_html/files/uploads/thumbs/')){
            echo "$path<br>";

                $work = new ImgResizer($temp);
                $work -> resize(300, $path);

        }
    }
         ?>

通知我有两个回声,

这就是他们记录的内容(对于第一项,其余错误看起来几乎相同):

/home/deia1/public_html/files/uploads/Detalle-Magrana.jpg

/home/deia1/public_html/files/uploads/thumbs/Detalle-Magrana.jpg

警告:imagecreatefrompng() 中的参数计数错误 /home/deia1/public_html/includes/thumbs.php 在第 21 行 /home/deia1/public_html/files/uploads/video.png /home/deia1/public_html/files/uploads/thumbs/video.png

警告:imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg:JPEG 库报告不可恢复的错误:在 /home/deia1/public_html/includes/thumbs.php 在第 13 行

警告:imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/home/deia1/public_html/files/uploads/video.png' 不是有效的 JPEG 在第 13 行的 /home/deia1/public_html/includes/thumbs.php 中的文件

知道我做错了什么吗?

-编辑-

我需要在 /thumbs/ 路径中保存调整大小的图像副本,我不想调整原始图像的大小。

-EDIT2-

这是我在使用@Bass Jobsen 为 PNG 文件建议的当前代码时遇到的错误,它们会生成白色图像。对于 jpg 似乎工作得很好:

Warning: imagepng() [function.imagepng]: gd-png: fatal libpng error: zlib failed to initialize compressor -- stream error in /home/deia1/public_html/includes/thumbs.php on line 60

Warning: imagepng() [function.imagepng]: gd-png error: setjmp returns error condition in /home/deia1/public_html/includes/thumbs.php on line 60

【问题讨论】:

标签: php image resize thumbnails


【解决方案1】:

根据你的代码重写你的类:

    class ImgResizer {
        var $originalFile = '$newName';
        function ImgResizer($originalFile = '$newName') {
            $this -> originalFile = $originalFile;
        }
        function resize($newWidth, $targetFile,$quality=90) {

            if (empty($newWidth) || empty($targetFile)) {
                return false;
            }


            if (($imgtype = exif_imagetype($this -> originalFile)) === IMAGETYPE_JPEG) {
                $src = imagecreatefromjpeg($this -> originalFile);
            }else if(($imgtype = exif_imagetype($this -> originalFile)) === IMAGETYPE_PNG){
                $src = imagecreatefrompng($this -> originalFile);
            }else if(($imgtype = exif_imagetype($this -> originalFile)) != IMAGETYPE_GIF){
                $src = imagecreatefromgif($this -> originalFile);
            }else die('invalid image format');


            list($width, $height) = getimagesize($this -> originalFile);
            $newHeight = ($height / $width) * $newWidth;
            $tmp = imagecreatetruecolor($newWidth, $newHeight);
            imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
            imagedestroy($src);


            if ($imgtype === IMAGETYPE_JPEG) {
                imagejpeg($tmp, $targetFile, $quality);
            }else if($imgtype === IMAGETYPE_PNG){
                //see: http://stackoverflow.com/a/7878801/1596547

                imagepng($tmp, $targetFile, (int)$quality*9/100);

            }else if($imgtype === IMAGETYPE_GIF){
                imagegif($tmp, $targetFile);
            }
            imagedestroy($tmp);

        }

    }


$work = new ImgResizer('./testdrive/bootstrap-150x150.png');
$work -> resize(30, './thumb.png');

【讨论】:

  • 嗨,'imagedestroy($temp)' 不会破坏原始图像吗?
  • 嘿,我编辑了我的问题,出现了 PNG 文件的错误,你知道吗?它会生成白色图像..
  • PNG 的质量是 0 到 10 的值,因此将 95 更改为 0.95 解决了它;)
  • 请参阅:stackoverflow.com/questions/7878754/creating-png-files 了解您的 png 错误或使用 imagepng($tmp, $targetFile, 9);imagepng 将图像写入文件。 imagedestroy 清除内存。
猜你喜欢
  • 2012-09-23
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多