1

我已经在我的页面上实现了图片的文件上传,并试图以某种方式生成缩略图,以便单击它们以通过 fancybox 进行查看。上传有效,但我创建缩略图的功能无效。(这包含在我的 upload.php 中,就在“move_uploaded_file”之后:

<?php
$src = $subdir.$fileupload['name'];

function make_thumb($src) 
{

$source_image = imagecreatefromjpeg($src);    //For testing purposes only jpeg now
$width = imagesx($source_image);
$height = imagesy($source_image);
$desired_width = 220;

$desired_height = floor($height * ($desired_width / $width));

$virtual_image = imagecreatetruecolor($desired_width, $desired_height);

imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

header("Content-type: image/jpeg");
imagejpeg($virtual_image, realpath('./Thumbnails/filename.jpg'));    //Temporary filename, will be changed
}
?>

仅供参考,这是一项作业,由于我是 php 初学者,我确实使用了 google,但在我的情况下找不到问题。可能是我对php的理解欠缺太多。

4

1 回答 1

0

使用此img_resize功能,它适用于最流行的图像格式

   function img_resize($src, $dest, $width, $height, $rgb = 0xFFFFFF, $quality = 100)
   {
        if (!file_exists($src))
            return false;

        $size = getimagesize($src);

        if ($size === false)
            return false;

        $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
        $icfunc = "imagecreatefrom" . $format;
        if (!function_exists($icfunc))
            return false;

        $x_ratio = $width / $size[0];
        $y_ratio = $height / $size[1];

        $ratio = min($x_ratio, $y_ratio);
        $use_x_ratio = ($x_ratio == $ratio);

        $new_width = $use_x_ratio ? $width : floor($size[0] * $ratio);
        $new_height = !$use_x_ratio ? $height : floor($size[1] * $ratio);
        $new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2);
        $new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);

        $isrc = $icfunc($src);
        $idest = imagecreatetruecolor($width, $height);

        imagefill($idest, 0, 0, $rgb);

        if (($format == 'gif') or ($format == 'png')) {
            imagealphablending($idest, false);
            imagesavealpha($idest, true);
        }

        if ($format == 'gif') {
            $transparent = imagecolorallocatealpha($idest, 255, 255, 255, 127);
            imagefilledrectangle($idest, 0, 0, $width, $height, $transparent);
            imagecolortransparent($idest, $transparent);
        }

        imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]);

        getResultImage($idest, $dest, $size['mime']);

        imagedestroy($isrc);
        imagedestroy($idest);

        return true;
    }

    function getResultImage($dst_r, $dest_path, $type)
    {
        switch ($type) {
            case 'image/jpg':
            case 'image/jpeg':
            case 'image/pjpeg':
                return imagejpeg($dst_r, $dest_path, 90);
                break;
            case 'image/png';
                return imagepng($dst_r, $dest_path, 2);
                break;
            case 'image/gif';
                return imagegif($dst_r, $dest_path);
                break;
            default:
                return;
        }
    }
于 2013-04-15T21:16:08.720 回答