【问题标题】:Create thumbnail after upload, PHP上传后创建缩略图,PHP
【发布时间】:2013-04-08 03:12:15
【问题描述】:

我已经为我的页面上的图片实现了文件上传,并试图以某种方式生成缩略图,以便通过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的了解太少了。

【问题讨论】:

  • 请启用错误报告,这样您就可以开始从 PHP 试图告诉您的问题中学习。
  • 出了什么问题?是有错误还是没有按预期运行?
  • 1.) 可以,需要先检查如何启用错误报告。 2.) 没有错误,什么都没有发生,我觉得这很奇怪,因为我的 php 经验伴随着很多错误消息
  • 所以,在启用错误报告 (error_reporting(E_ALL);) 之后,什么都没有弹出。所以我猜有些东西在起作用,只是不像我想要的那样。

标签: php image upload thumbnails


【解决方案1】:

使用这个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;
        }
    }

【讨论】:

  • 你能解释一下是什么问题吗?
  • @JuanMendes 这不是问题,这是决定:)
  • 整个 mime/function_exists 的东西毫无意义,可能只是 imagecreatefromstring(readfile($src))
  • @MarcB imagecreatefromstring 有图像透明区域的问题
猜你喜欢
  • 2012-09-21
  • 2013-05-31
  • 2019-06-08
  • 2021-04-24
  • 2017-03-19
  • 1970-01-01
  • 2011-08-11
  • 2016-05-10
  • 2016-04-11
相关资源
最近更新 更多