javascript - How to reduce Image file size without losing quality or resolution using php or any other language? -
i have 1 mb hd resolution image. want file size reduce 50% min (512kb). have tried using php it's not working me. if 1 knows please me.
like https://tinyjpg.com/ website.
$filename = "image.jpg"; // 1 mb image file header('content-type: image/jpeg'); list($width_orig, $height_orig) = getimagesize($filename); $width = $width_orig; $height = $height_orig; $ratio_orig = $width_orig / $height_orig; if ($width / $height > $ratio_orig) { $width = $height * $ratio_orig; } else { $height = $width / $ratio_orig; } $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); imagejpeg($image_p, "image_resized.jpg", 100);
function compress_image($source_url, $destination_url, $quality) {
$info = getimagesize($source_url); if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url); elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url); elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url); imagejpeg($image, $destination_url, $quality); return $destination_url; }
Comments
Post a Comment