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.