首页 文章

没有为所有 mysql post img 附件创建大拇指

提问于
浏览
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/thumbs/video.png

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

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

知道我做错了什么吗?

-EDIT-

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

-EDIT2-

这是我为 PNG 文件建议的当前代码 that_1_Jobsen 得到的错误,它们生成白色图像。对于 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

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');
    

相关问题