yuvi
yuvi

Reputation: 52

How can i fit a image into a frame without losing ratio using PHP

Actually, Here user can upload any size image to fit into any frame which i have. I want to fit that image into a frame without losing ratio.

Example:

This image is not fitting with my php code.

My code is

list($width, $height) = getimagesize($img1);
        $outputImage = imagecreatetruecolor($width, $height);

        // set background to white
        //$white = imagecolorallocate($outputImage, 255, 255, 255);
        //imagefill($outputImage, 0, 0, $white);
        list($width1, $height1) = getimagesize($img2);
        $info1 = getimagesize($img1);
        $info2 = getimagesize($img2);
        $extension1 = image_type_to_extension($info1[2]);
        $extension2 = image_type_to_extension($info2[2]);
        if($extension1=='.jpeg') {
        $first = imagecreatefromjpeg($img1);
        } else { 
        $first = imagecreatefrompng($img1);
        }
        if($extension2=='.jpeg') {
        $second = imagecreatefromjpeg($img2);
        } else { 
        $second = imagecreatefrompng($img2);
        }
        $ratio = $width1 / $height1;
        $targetWidth = 300 * $ratio;
        //echo $targetWidth;
        //imagecopyresized ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
        imagecopyresampled($outputImage,$first,0,0,0,0, $width, $height, $width, $height);
        imagecopyresampled($outputImage,$second,110,75,0,0, $targetWidth, 300, $width1, $height1);

Please help me. Thanks in advance.

Upvotes: 0

Views: 697

Answers (1)

Emad Elpurgy
Emad Elpurgy

Reputation: 357

to achieve that you must detect the frame image and source image dimensions re size the source image to fit the frame image dimensions. note that to avoid ratio problems we will detect if the source image wide image or tall image (landscape or portrait) if width > height then set the width to the width of frame and calculate the new height based on main ratio if height > width set height to frame height and calculate the new width this is sample implementation code

<?
$src  = imagecreatefromjpeg('source.jpg');//source image path
$frame = imagecreatefrompng('frame.png');//frame image
$sourceWidth = imagesx($src); // source image width
$sourceHeight = imagesy($src); // image image height
$frameWidth = imagesx($frame);//get frame width
$frameHeight = imagesy($frame);//get frame height
$NewImage = imagecreatetruecolor($frameWidth,$frameHeight);//the new image
$bg_color = imagecolorallocate ($NewImage, 255, 255, 255);
imagefill($NewImage, 0, 0, $bg_color);//set background to white
if($sourceHeight >= $sourceWidth){
$newHeight=$frameHeight;// set height to frame height
$newWidth=intval($frameWidth*$sourceWidth/$sourceHeight);//calculate the new width  
imagecopyresampled($NewImage, $src, intval(($frameWidth-$newWidth)/2),0,0,0,$newWidth, $newHeight, $sourceWidth, $sourceHeight);//insert source image to new image at the center
} else {
$newWidth=$frameWidth;;// set width to frame width
$newHeight=intval($frameHeight*$sourceHeight/$sourceWidth);//calculate the new height 
imagecopyresampled($NewImage, $src,0,intval(($frameHeight-$newHeight)/2),0,0,$newWidth, $newHeight,$sourceWidth, $sourceHeight);//insert source image to new image at the center
}
imagecopyresampled($NewImage, $frame, 0,0, 0,0, $frameWidth, $frameHeight, $frameWidth, $frameHeight);//copy frame imageto new image 
header("Content-type: image/png");
imagejpeg($NewImage);
?>

hope this would help.

Upvotes: 3

Related Questions