可以';t让图像调整大小的代码在PHP中工作


Can't get image resize code to work in PHP

我正在构建一个缩略图系统,但我的代码输出了一堆或随机的图标。它显然应该起作用,但事实并非如此。

代码:

<?php
// The file
$filename = 'post/imgres/posts/1.jpg';
// Set a maximum height and width
$width = 200;
$height = 200;
// Content type
header('Content-Type: image/jepg');
// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,     $width_orig, $height_orig);
// Output
imagejpeg($image_p, null, 100);
?>

我在claviska中找到了一个名为simpleimage的类。我的代码现在完成了。一切看起来都是这样的。

处理缩略图的代码:

    function isThumb($thumbnail)
    {
        if (file_exists($thumbnail))
        {
            return true;
        }
        return false;
    }
    function makeThumb($original, $destination)
    {
        $img = new abeautifulsite'SimpleImage($original);
        $img->fit_to_width(256);
        $img->save($destination);
    }
    ?>

加载图像的代码:

        <?php
            //Image loader
            //Folder containing images.
            $filedir = "imgres/posts/";
            //Retrieve the images in the folder.
            $images = glob($filedir."*.{jpg,JPG,png,PNG,gif,GIF}",GLOB_BRACE);
            //Make sure the image array is not empty, or null.
            if (!empty($images))
            {
                //Load the images into the website.
                foreach ($images as $image)
                {
                    if (pathinfo($image,PATINFO_EXTENSION) == "gif" || pathinfo($image, PATHINFO_EXTENSION)=="GIF") 
                    {
                        echo '<a href="'.$image.'"><img src="'.$image.'"/></a>';
                    }
                    else
                    {
                        echo '<a href="'.$image.'"><img src="'.getThumb($image).'"/></a>';
                    }
                }
            }
            else
            {
                //Write out an error message to warn the user.
                echo "<p>No images were found in the server. This is most likely an error in the PHP code, incompatibility, or something went wrong with our storage solution. Contact admin! Information needed: Browser, OS, and has the site worked before?</p>";
            }
        ?>

PHP图像图钉是一项常见的任务,有很多扎实的图像图钉示例和源代码。我不确定你的剧本在做什么,但这里有一个基本的剧本,它总是有效的,而且是直截了当的。

您应该涵盖并支持所有图像类型png和gif。但既然你在做jpegs,现在就用它吧。添加对其他图像类型的支持应该没有那么困难。

/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width  */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
// Content type, you need this line of code to 
// display image to browser or for testing it on the UI
header('Content-Type: image/jepg');
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);