【问题标题】:Problem with thumbnails after newly added - PHP on-the-fly method新添加后的缩略图问题 - PHP on-the-fly 方法
【发布时间】:2011-02-25 18:53:55
【问题描述】:

对于那些可能已经阅读了我在几分钟前得到解决的上一个问题的人>。

on-the-fly php 脚本完美运行,但是当我将新图像上传到我自己制作的画廊时,图像被调整为 150 x 150 以达到我想要的大小......但是,当它出现时添加的新图像全是黑色的......

如您所见,上传到文件夹的三个黑色图像和添加到数据库的目录。

其他(非黑色图像)已使用 image.php 调整大小。

这是什么原因造成的?

如果我查看源代码,代码很好...... PHP 中的 while 循环会生成如下输出:

<div class="view-wrap" id="photo-10">
    <div class="view-icon">
        <div class="img-label">
            <a href="#" id="10" class="delete"><img src="img/small-delete.png" /> Delete</a>
        </div>

        <a href="img/events/Paintballing/24251_1395408043148_1170626626_1204038_5382765_n.jpg">
            <img src="image.php?dir=img/events/Paintballing/24251_1395408043148_1170626626_1204038_5382765_n.jpg" alt="" width="110" height="110" />
        </a>
    </div>
</div>

一个块的例子。

如果我查看源代码(在 Firefox 中)并单击 image.php?dir=img/events/Paintballing/24251_1395408043148_1170626626_1204038_5382765_n.jpg 示例,我可以看到 150 x 150 大小的缩略图,但在布局中,它显示一个黑色缩略图...

有人知道为什么会这样吗?

编辑:

<?php 

$dir = $_GET['dir'];

header('Content-type: image/jpeg'); 

$create = imagecreatetruecolor(150, 150); 

$img = imagecreatefromjpeg($dir);

list($width, $height) = getimagesize($dir);

imagecopyresampled($create, $img, 0, 0, 0, 0, 150, 150, $width, $height);

imagejpeg($create, null, 100); 

?>

这是 image.php。

【问题讨论】:

    标签: php image-processing thumbnails


    【解决方案1】:

    感谢您更新您的帖子。

    您确定图片是您上传的 jpg/jpeg。

    尝试更改为以下

    <?php 
    
    $dir = $_GET['dir'];
    $ext = strtoupper(pathinfo($dir, PATHINFO_EXTENSION));
    switch($ext)
    {
        case 'jpeg':
        case 'jpg':
            $img = imagecreatefromjpeg($dir);
        break;
        case 'png':
            $img = imagecreatefrompng($dir);
        break;
        case 'gif':
            $img = imagecreatefromgif($dir);
        break;
    }
    if(isset(img))
    {
        header('Content-type: image/jpeg'); 
        $create = imagecreatetruecolor(150, 150); 
        list($width, $height) = getimagesize($dir);
        imagecopyresampled($create, $img, 0, 0, 0, 0, 150, 150, $width, $height);
        imagejpeg($create, null, 100); 
    }else
    {
       echo sprintf('Unable to process image, Unknown format %s',$ext);
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      为什么不添加边框而不是拉伸图像?

      这是函数

          function resize_to_canvas($filename,$canvas_w=100,$canvas_h=225){
      list($width, $height, $type) = getimagesize($filename);
      
      $original_overcanvas_w = $width/$canvas_w;
      $original_overcanvas_h = $height/$canvas_h;
      
      $dst_w = round($width/max($original_overcanvas_w,$original_overcanvas_h),0);
      $dst_h = round($height/max($original_overcanvas_w,$original_overcanvas_h),0);
      
      $dst_image = imagecreatetruecolor($canvas_w, $canvas_h);
      $background = imagecolorallocate($dst_image, 255, 255, 255);
      imagefill($dst_image, 0, 0, $background);
      
      $src_image = imagecreatefromjpeg($filename);
      imagecopyresampled($dst_image, $src_image, ($canvas_w-$dst_w)/2, ($canvas_h-$dst_h)/2, 0, 0, $dst_w, $dst_h, $width, $height);
      imagegif($dst_image, $filename);
      imagedestroy($dst_image);}
      

      此功能将替换原始文件,但很容易修改以创建新的缩略图。只需更改文件名行 imagegif($dst_image, $filename);

      【讨论】:

        猜你喜欢
        • 2010-12-18
        • 1970-01-01
        • 2021-06-10
        • 2023-03-06
        • 1970-01-01
        • 2011-10-22
        • 1970-01-01
        • 1970-01-01
        • 2016-02-06
        相关资源
        最近更新 更多