简体   繁体   中英

How to compress image file size laravel

I have some images in my S3 bucket on AWS. I am trying to get the images and send them to my android app (along with other information from my database). My current setup works fine but I would like to reduce the image file sizes (currently around 1-1.5mb) before I send them to the app.

I have tried to use this code:

function compress($source, $destination, $quality) {

    $info = getimagesize($source);

    if ($info['mime'] == 'image/jpeg') 
        $image = imagecreatefromjpeg($source);

    elseif ($info['mime'] == 'image/gif') 
        $image = imagecreatefromgif($source);

    elseif ($info['mime'] == 'image/png') 
        $image = imagecreatefrompng($source);

    imagejpeg($image, $destination, $quality);

    return $destination;
}

$image_file = Storage::disk('s3_upload')->get("s3bucketpath");

$new_image_file = $this->replace_extension($image->filename, '.jpg');

$this->compress($image_file,$new_image_file, 50);

I get

failed to open stream: No such file or directory in file

error from

$info = getimagesize($source);

I have checked the file path and it is in the bucket and exists and I have var-dumped source and gotten

�����JFIF���������C�$<'$!!$J58,<XM\[VMUSam�vag�hSUy�z������^t������������C$ $G''G�dUd�����������������������������������������������������@0"�����������������A�����!1A"Q2aq#B���3R�$4b�rC���%S���5D������������������#�����!1A2"Q#�����?���HS-�� ��� ��B�@�)��(  @������
@����!@��������������(�����(����� �
@����UF�LHU@�d ��
@ �����)������@R��B����(�R���E]��(

(� @B'...

What is the best way to go about doing this.

Try http://image.intervention.io/ to compress file. It is simple and efficient.

Compression details: http://image.intervention.io/api/encode

How to start? Simply:

// include composer autoload
require 'vendor/autoload.php';

// import the Intervention Image Manager Class
use Intervention\Image\ImageManager;

// create an image manager instance with favored driver
$manager = new ImageManager(array('driver' => 'imagick'));

// to finally create image instances
$image = $manager->make('public/foo.jpg')->resize(300, 200);

如果您真的很想压缩和调整图像大小,则可以使用PNGQuant之类的进行PNG compression而不会降低质量, 对于jpeg图像则不要使用jpegoptim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM