Reputation: 21
How can I resize $image_2 & $iamge_3 To be 350x350 without aspect ratio.
I want it to fits two transparency space made 350x350.
<?php
$imageFile = base_url('upload/users/images/').image_to_thumb($user->image); // 133 x 133
$watermarkFile = base_url('upload/users/images/').image_to_thumb($user->image); // 133 x 133
$bgFile = base_url('/backgroundtest.png'); // 93 x 93
// We want our final image to be 76x76 size
$x = 1200;
$y = 630;
// dimensions of the final image
$final_img = imagecreatetruecolor($x, $y);
// Create our image resources from the files
$image_1 = imagecreatefrompng($imageFile);
$image_2 = imagecreatefromjpeg($watermarkFile);
$image_3 = imagecreatefrompng($bgFile);
// Enable blend mode and save full alpha channel
imagealphablending($final_img, true);
imagesavealpha($final_img, true);
imagecopy($final_img, $image_1, 200, 200, 0, 0, 350, 350);
imagecopy($final_img, $image_2, 200, 200, 0, 0, 350, 350);
imagecopy($final_img, $image_3, 0, 0, 0, 0, 1200, 630);
imagepng($final_img, encode_id($user->id).'.png');
?>
Upvotes: 1
Views: 26
Reputation: 21
I used This and works well.
$file = $_FILES['img']['tmp_name'];
$maxW = $maxH = 400;
list($srcW, $srcH) = getimagesize($file);
$ratio = $srcW / $srcH;
$src = imagecreatefromjpeg($file);
$dest = imagecreatetruecolor($maxW, $maxH);
if ($ratio > 1) {
// landscape.
$destH = ($maxH / $ratio);
imagecopyresized($dest, $src, 0, ($maxH / 2) - ($destH / 2), 0, 0, $maxW, $destH, $srcW, $srcH);
} else {
// portrait (or square).
$destW = ($maxW * $ratio);
imagecopyresized($dest, $src, ($maxW / 2) - ($destW / 2), 0, 0, 0, $destW, $maxH, $srcW, $srcH);
}
// now do whatever you want with $dest...
Upvotes: 1