1

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

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

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

 <?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

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

警告:imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /home/deia1/public_html/includes/thumbs.php on line 13

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

知道我在做什么错吗?

-编辑-

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

-编辑2-

这是我使用 @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
4

1 回答 1

2

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

    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');
于 2013-08-12T10:08:37.610 回答