PHP上传后创建缩略图

时间:2013-04-15 21:12:09

标签: php image upload thumbnails

我已经在我的页面上实现了图片文件上传,并尝试以某种方式生成缩略图,以便点击它们通过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初学者,我确实使用谷歌,但在我的情况下找不到问题。也许我对php的理解缺乏太多。

1 个答案:

答案 0 :(得分: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;
        }
    }