<? php
//Specify the original picture path and output picture path
$src = 'original_image.jpg' ; $out = 'compressed_image.jpg' ; //Specify the width and height of the target picture
$targetWidth = eight hundred ; $targetHeight = six hundred ; //Create an image object
$img = imagecreatefromjpeg ( $src ); //Get the width and height of the original picture
$srcWidth = imagesx ( $img ); $srcHeight = imagesy ( $img ); //Calculate Scaling
$scale = min ( $targetWidth / $srcWidth , $targetHeight / $srcHeight ); //Calculate the width and height of the target image
$newWidth = round ( $srcWidth * $scale ); $newHeight = round ( $srcHeight * $scale ); //Create a new blank picture object
$newImg = imagecreatetruecolor ( $newWidth , $newHeight ); //Copy and resize image
imagecopyresampled ( $newImg , $img , zero , zero , zero , zero , $newWidth , $newHeight , $srcWidth , $srcHeight ); //Export to file
imagejpeg ( $newImg , $out ); //Free memory
imagedestroy ( $img ); imagedestroy ( $newImg ); ?>