Php使用图像gd调整大小不显示图像但调整大小

时间:2014-04-05 18:41:10

标签: php image gd

我在这个问题上做了很多搜索,但仍然...... 我有一个PHP代码来调整图库Gd的图像,但问题是,调整大小后它不会显示图像。 这是代码

文件名resize.php

<?php
/*
 * PHP function to resize an image maintaining aspect ratio
 * http://salman-w.blogspot.com/2008/10/resize-images-using-phpgd-library.html
 *
 * Creates a resized (e.g. thumbnail, small, medium, large)
 * version of an image file and saves it as another file
 */

define('THUMBNAIL_IMAGE_MAX_WIDTH', 150);
define('THUMBNAIL_IMAGE_MAX_HEIGHT', 150);

function generate_image_thumbnail($source_image_path, $thumbnail_image_path)
{
    list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
    switch ($source_image_type) {
        case IMAGETYPE_GIF:
            $source_gd_image = imagecreatefromgif($source_image_path);
            break;
        case IMAGETYPE_JPEG:
            $source_gd_image = imagecreatefromjpeg($source_image_path);
            break;
        case IMAGETYPE_PNG:
            $source_gd_image = imagecreatefrompng($source_image_path);
            break;
    }
    if ($source_gd_image === false) {
        return false;
    }
    $source_aspect_ratio = $source_image_width / $source_image_height;
    $thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH / THUMBNAIL_IMAGE_MAX_HEIGHT;
    if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) {
        $thumbnail_image_width = $source_image_width;
        $thumbnail_image_height = $source_image_height;
    } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
        $thumbnail_image_width = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
        $thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
    } else {
        $thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
        $thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH / $source_aspect_ratio);
    }
    $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
    imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
    imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90);
    imagedestroy($source_gd_image);
    imagedestroy($thumbnail_gd_image);
    return true;
}

generate_image_thumbnail('sql/max.jpg', 'sql/thumbnail_image_width_max.jpg');
?>

在index.php中,我做了类似的事情

<img src="resize.php"/> 

问题是它调整了图像的大小,但是当我执行<img src="resize.php"/>时它不会显示它,但图像存在,如<img src="sql/thumbnail_image_width_max.jpg"/>中存在。但是,如果没有<img src="resize.php"/>,我甚至不知道如何调整图像大小,以便我可以直接用<img src="sql/thumbnail_image_width_max.jpg"/>显示它或直接用<img src="resize.php"/>.显示它

感谢您的帮助。并且希望这次我的问题不再是减号。

1 个答案:

答案 0 :(得分:1)

让我在这里帮助你。 该功能工作正常,所以,我认为你的问题在index.php。 在该文件中添加:

include("resize.php"); //to include the function.

然后调用该函数并检查是否正常工作。

$result=generate_image_thumbnail('sql/max.jpg', 'sql/thumbnail_image_width_max.jpg');

if($result)
echo '<img src="sql/thumbnail_image_width_max.jpg"/>';
else
echo "error in image function";