【问题标题】:Laravel 5.6: Create image thumbnailsLaravel 5.6:创建图像缩略图
【发布时间】:2018-10-31 06:59:14
【问题描述】:

在我的旧 PHP 应用程序中,我曾经运行类似下面的函数来创建 jpeg 图像缩略图。

function imageThumbanail() {

 $image_src = imagecreatefromjpeg('http://examplesite.com/images/sample-image.jpg'); 

 $thumbnail_width = 180; //Desirable thumbnail width size 180px

 $image_width = imagesx($image_src); //Original image width size -> 1080px

 $image_height = imagesy($image_src); //Original image height size -> 1080px

 $thumbnail_height = floor( $image_height * ( $thumb_width / $image_width ) ); //Calculate the right thumbnail height depends on given thumbnail width

 $virtual_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);

 imagecopyresampled($virtual_image, $image_src, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $image_width, $image_height);

 header('Content-Type: image/jpeg');

 imagejpeg($virtual_image, null, 100);

 imagedestroy($virtual_image); //Free up memory

}

问题是现在我想在 laravel 5.6 应用程序中运行类似的功能,所以我创建了一个具有完全相同功能的控制器,但我没有将图像缩略图作为输出,而是得到问号和奇怪的菱形图标,类似于 php gd 库 jpeg 图像的编码版本。

我尝试使用 return response()->file($pathToFile);正如 laravel 文档所述,但我不会将缩略图存储在某个位置。

有什么想法吗?

提前谢谢你!

【问题讨论】:

标签: php laravel image thumbnails gd


【解决方案1】:

我向你推荐这个包安装和使用非常简单,并且非常适合编程。 它被称为干预

Intervention package to handle images

您可以制作一个非常简单的缩略图,如下所示:

$img = Image::make('public/foo.jpg')->resize(320, 240)->insert('public/watermark.png');

【讨论】:

    【解决方案2】:

    按中心调整和裁剪图像

    无需安装和包含任何软件包。只需在 laravel / lumen 中创建一个 helper 并将以下代码放入该 helper 文件中,然后在任何你想要的地方使用它:

    function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 80){
        $imgsize = getimagesize($source_file);
        $width = $imgsize[0];
        $height = $imgsize[1];
        $mime = $imgsize['mime'];
    
        switch($mime){
            case 'image/gif':
                $image_create = "imagecreatefromgif";
                $image = "imagegif";
                break;
    
            case 'image/png':
                $image_create = "imagecreatefrompng";
                $image = "imagepng";
                $quality = 7;
                break;
    
            case 'image/jpeg':
                $image_create = "imagecreatefromjpeg";
                $image = "imagejpeg";
                $quality = 80;
                break;
    
            default:
                return false;
                break;
        }
    
        $dst_img = imagecreatetruecolor($max_width, $max_height);
        $src_img = $image_create($source_file);
    
        $width_new = $height * $max_width / $max_height;
        $height_new = $width * $max_height / $max_width;
        //if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
        if($width_new > $width){
            //cut point by height
            $h_point = (($height - $height_new) / 2);
            //copy image
            imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
        }else{
            //cut point by width
            $w_point = (($width - $width_new) / 2);
            imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
        }
    
        $image($dst_img, $dst_dir, $quality);
    
        if($dst_img)imagedestroy($dst_img);
        if($src_img)imagedestroy($src_img);
    }
    //usage example
    resize_crop_image(100, 100, "test.jpg", "test.jpg");
    

    代码已经过多次测试并且运行良好。一切顺利,节省您的时间,享受您的生活:)

    【讨论】:

      猜你喜欢
      • 2017-06-02
      • 2014-11-25
      • 1970-01-01
      • 2011-02-09
      • 2016-04-19
      • 1970-01-01
      • 2019-01-29
      • 2010-09-27
      • 2012-09-21
      相关资源
      最近更新 更多