1

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.

user3074140
  • 733
  • 3
  • 13
  • 30

3 Answers3

2

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);
Adam Kozlowski
  • 5,606
  • 2
  • 32
  • 51
1

If you are really serious about compressing and resizing your images, you can use libraries like PNGQuant for PNG compression without losing the quality and jpegoptim for jpeg images.

Dexter Bengil
  • 5,995
  • 6
  • 35
  • 54
0

check out this soloution Pls

How to compress image before uploading in Laravel?

here is the library suggested in case you don't want to go to this page first

https://github.com/spatie/laravel-image-optimizer

Badmous
  • 95
  • 1
  • 5