新添加后缩略图出现问题 - PHP 即时方法

发布于 2024-09-03 16:53:43 字数 1589 浏览 0 评论 0原文

对于那些可能已经阅读过我几分钟前解决的问题的人>。<

动态 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 中)并单击 exmaple 的 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。

to those that may have read my previous question that got solved a couple minutes ago >.<

The on-the-fly php script works perfectly, but when I went to upload new images into a gallery which I made myself, the images are resized to 150 x 150 for what I wanted... however, when it comes to new images being added it is all black...

alt text

As you can see the three black images that were uploaded to the folder and the directory added to the database.

The other (non-black images) are already resized with image.php.

What is causing this?

If I view the source, the code is fine... the while loop in the PHP generates an output like this:

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

An example of one block.

If I view the source (in Firefox) and click on the image.php?dir=img/events/Paintballing/24251_1395408043148_1170626626_1204038_5382765_n.jpg by exmaple, I can see the thumbnail at it's 150 x 150 size but in the layout, it shows a black thumbnail...

Does anyone know why this is happening?

EDIT:

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

?>

This is image.php.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浴红衣 2024-09-10 16:53:43

感谢您更新您的帖子。

您确定该图像是您上传的 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);
}
?>

Thanks for updating your post.

Are you positive that the image is a jpg/jpeg that your uploading.

Try changing to the following

<?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);
}
?>
大姐,你呐 2024-09-10 16:53:43

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

这是函数

    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); 行即可

Rather than stretching the images why not add a border?

Here's the function

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

This function will replace the original file but is easily modified to create a new thumbnail image. Just change the file name the line imagegif($dst_image, $filename);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文