1

I want to compress images before I upload them to my s3. This is the code I'm using to compress the images.

//compress image before uploading
    public static function compressedImage($img, $file_name, $ext){
        $original_img;
        $img_size = getimagesize($img);

        $original_width = $img_size[0];
        $original_height = $img_size[1];
        $mime_type = $img_size['mime'];

        $desired_width = 450;
        $desired_height = ( $original_height * $desired_width ) / $original_width;

        // making a copy of the image
        if($mime_type=="image/jpeg"){
            header('Content-Type: image/jpeg');
            $original_img = imagecreatefromjpeg($img);
        }elseif($mime_type=="image/png"){
            header('Content-Type: image/png');
            $original_img = imagecreatefrompng($img);
        }elseif($mime_type=="image/gif"){
            header('Content-Type: image/gif');
            $original_img = imagecreatefromgif($img);
        }

        try {
            $temp_img = imagecreatetruecolor($desired_width, $desired_height);
            $copy_img = imagecopyresampled($temp_img, $img, 0, 0, 0, 0, $desired_width, $desired_height, $original_width, $original_height);                
            $compressed = imagejpeg($temp_img, public_path() . "/uploads/" . $file_name . "." . $ext,100);
            return public_path() . "/uploads/" . $file_name . "." . $ext;   
        } catch (Exception $e) {
            return $e;
        }
    }

This compression works perfectly on my local server but for some reason is not working on the online server.

After going through it, I realized the problem seems to be with the imagecreatefrom...() section but I can't seem to figure out exactly what is wrong with it. I will be very happy if someone could help.

miken32
  • 42,008
  • 16
  • 111
  • 154
Osei-Bonsu Christian
  • 2,935
  • 1
  • 15
  • 8
  • basic debugging: all of the `image*()` functions return boolean false on failure. start checking for that false. – Marc B May 20 '15 at 21:06
  • Thanks for your reply @MarcB but I don't think that's the problem. I checked for that and for all those it met either the jpeg or png condition but the imagecreatefrompng/imagecreatefromjpeg function doesn't seem to work as it's supposed to. Like I said, it works locally though – Osei-Bonsu Christian May 20 '15 at 22:01

0 Answers0