GD Image overlay transparent png - php

I have transparent image that I want to overlay over the entire image to give it the boarder effect. In this code it crops an existing image. It merges the two but the final over the top is not showing the alpha. How can I fix this?
<?
$dst_x = 0; // X-coordinate of destination point.
$dst_y = 0; // Y --coordinate of destination point.
$src_x = 163; // Crop Start X position in original image
$src_y = 0; // Crop Srart Y position in original image
$dst_w = 469; // Thumb width
$dst_h = 296; // Thumb height
$src_w = 469; // $src_x + $dst_w Crop end X position in original image
$src_h = 296; // $src_y + $dst_h Crop end Y position in original image
// Creating an image with true colors having thumb dimensions.( to merge with the original image )
$dst_image = imagecreatetruecolor($dst_w,$dst_h);
// Get original image
$src_image = imagecreatefromjpeg("http://www.ucatholic.com/wp-content/uploads/2012/10/Sallixtus-631x295.jpg");
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, "images/crop.jpg");
$src = imagecreatefrompng('http://www.EdVizenor.com/images/saintCover.png');
/// THIS LINE NOT WORKING - - // Copy and merge
imagecopymerge($dst_image, $src, 0, 0, 0, 0, 469, 296, 100);
header('Content-Type: image/png');
imagegif($dst_image);
?>

Dont use imagecopymerge, use imagecopy, then it will works normally and your watermark will be shown with alpha.
imagecopy($dst_image, $src, 0, 0, 0, 0, 469, 296);

If used the imagecopy instead of imagecopymerge you will lose control over alpha channel.
I think you could use Jaguar , if you want a real fast overlay action using the Gd extension.
see my answer Here if you want to see how
if you want a brushed border you can use Jaguar like this:
use Jaguar\Canvas,
Jaguar\Drawable\Border;
$canvas = new Canvas('your image');
$brush = new Canvas('style image');
$border = new Border(20);
$border->draw($canvas,$brush);
$canvas->save('path to save')->show();

Related

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.
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.

How can I crop an image properly with php?

I am trying to crop an image on PHP. I saw that there is a function for doing that on PHP: imagecrop. Here is the code in which I am trying to crop the image.
$image = imagecreatefromjpeg(//Path of the image);
$croppedImage = imagecrop($image, array("x"=>0,"y"=>0,"width"=>100,"height"=>100));
imagejpeg($croppedImage, //Path in which the image will be stored);
Here I want that the crop of the image starts in the left corner of the image and get the values of width and height that I put above.
But it just resize my image, not crop it. What am I doing wrong?
EDIT:
I tried also with the function imagecopyresampled. Here it is what I have tried:
dst_image(Destination image link resource) = newImage; //Here the new image that I want to create.
src_image(Source image link resource) = image; //Here the original image.
//Here 0,0 because I want that the new image crop starts in the left corner of the original image.
dst_x(x-coordinate of destination point) = 0;
dst_y(y-coordinate of destination point) = 0;
//Here 0,0 because I want that the crop starts on the 0,0 of the original image
src_x(x-coordinate of source point) = 0;
src_y(y-coordinate of source point) = 0;
dst_w(Destination width) = 150; //The new width of the crop image.
dst_h(Destination height) = 150; //The new height of the crop image.
src_w(Source width) = 500; //The original width of the image.
src_h(Source height) = 500; //The original height of the image.
So finally the function will be like:
$b = imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w , $dst_h , $src_w , $src_h );
I just have problems in this function it is why avoid the rest (save and imagetruecolor and the rest...)
This function give to me the result expected but the new image it is black, why?
Thanks in advance!
try this:
$dst_x = 0; // X-coordinate of destination point
$dst_y = 0; // Y-coordinate of destination point
$src_x = 100; // Crop Start X position in original image
$src_y = 100; // Crop Srart Y position in original image
$dst_w = 160; // Thumb width
$dst_h = 120; // Thumb height
$src_w = 260; // $src_x + $dst_w Crop end X position in original image
$src_h = 220; // $src_y + $dst_h Crop end Y position in original image
// Creating an image with true colors having thumb dimensions (to merge with the original image)
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// Get original image
$src_image = imagecreatefromjpeg('images/cropped_whatever.jpg');
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, 'images/crop.jpg');
Alternatively you could use the imagecrop function of imagick.
Finally, I got the solution of my problem. I had to put the same value of dst_w and src_w. The same to dst_h and src_h. ¡¡And it works!!
The final solution it is the following:
$b = imagecopyresampled ($dst_image, $src_image, 0, 0, 0, 0, 150, 150, 150, 150);

Imagecopy not working properly

I have been trying to implement a rating display procedure to rating in form of stars as passed by GET in a php file.
Here's the code for rate.php :
$filename= "rating.png";
$rating = $_GET['rating'];
list($w, $h, $type, $attr) = getimagesize($filename);
$src_im = imagecreatefrompng($filename);
$dst_im = imagecreatefrompng("rating_back.png");
$src_x = '0'; // begin x
$src_y = '0'; // begin y
$src_w = $w * $rating / 5; // width
$src_h = $h; // height
$dst_x = '0'; // destination x
$dst_y = '0'; // destination y
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
header("Content-type: image/png");
imagepng($dst_im);
imagedestroy($dst_im);
$rating is a float value as per the GET request.
But when I try to implement rate.php?rating=4.3 using the following images:
"rating.png":
"rating_back.png":
I get the following result :
What could be the possible errors which are leading to an abnormal background in the final image?
Please give the possible solutions also.
Thanks in advance :)
It looks like those are 24 bit PNG images with transparency. You need to tell GD to save the transparency info too. Add:
imagesavealpha($dst_im, true);
before rendering your image

put large image on small image in php

I am rookie at php.
$qrImage = imagecreatefrompng($filename);
$tempImage = imagecreatefrompng("image/facebook.png");
$LogoImage = imagecreatetruecolor($matrixPointSize*5, $matrixPointSize*5);
// Resize Logo.
imagecopyresampled($LogoImage, $tempImage, 0, 0, 0, 0, imagesx($LogoImage), imagesy($LogoImage), imagesx($tempImage), imagesy($tempImage));
$src_x = (imagesx($qrImage)/2)-(imagesx($LogoImage)/2);
$src_y = (imagesy($qrImage)/2)-(imagesy($LogoImage)/2);
imagecopymerge($qrImage, $LogoImage, $src_x, $src_y, 0, 0, $matrixPointSize*5, $matrixPointSize*5, 90);
header("Content-Type: image/png");
imagepng($qrImage);
Using this code image look like
I want to put logo on the background of the qrCode image.
and is there any way to save this image as svg file??
Can anybody help me??

Scaling a watermark to fit parent image

My photo sizes vary, they are either landscape, portrait or square, and I need to make a watermark the best fit for each photo - so I need to resize just the width of the watermark (without Imagick), as it's a long rectangle shape, so height doesn't matter.
I found the PHP function, imagecopyresized, but I'll be honest, I can't work out what parameters are needed for my situation, even after looking at PHP documentation! I'm also not sure if after using imagecopyresized, the rest of my function will work where it gets the watermark width and height.
Can somebody help me get over the finish line. This is how far I got, all it needs is the right parameters added to the imagecopyresized part:
<?php
header('content-type: image/jpeg');
$image = imagecreatefromjpeg('https://.....jpg');
$imageSize = getimagesize('https://.....jpg');
$newWatermarkWidth = $imageSize[0]-50; // width of image minus 50px
$watermark = imagecreatefrompng('watermark.png');
// resize watermark to newWatermarkWidth here with imagecopyresize
$watermark = imagecopyresized(?,?,?,?);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = ($imageSize[0]/2) - ($watermark_width/2) ;
$dest_y = ($imageSize[1]/2) - ($watermark_height/2);
imagecopy($image, $watermark, round($dest_x,0), round($dest_y,0), 0, 0, $watermark_width, $watermark_height);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
THIS IS WHAT I ENDED WITH & WORKS PERFECTLY
A script that adjusts the width of a watermark to fit across the whole parent image, centered and proportional.
<?php
header('content-type: image/jpeg');
$image = imagecreatefromjpeg('http://mydomain.com/myPhoto.jpg');
$imageSize = getimagesize('http://mydomain.com/myPhoto.jpg');
$watermark = imagecreatefrompng('http://mydomain.com/myWatermark.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
$newWatermarkWidth = $imageSize[0]-20;
$newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;
imagecopyresized($image, $watermark, $imageSize[0]/2 - $newWatermarkWidth/2, $imageSize[1]/2 - $newWatermarkHeight/2, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($watermark), imagesy($watermark));
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
This resizes the watermark and copies directly to the image.
You don't need the existing imagecopy line anymore.
$success = imagecopyresized($image, // Destination image
$watermark, // Source image
$imageSize[0]/2 - $newWatermarkWidth/2, // Destination X
$imageSize[1]/2 - imagesy($watermark)/2, // Destination Y
0, // Source X
0, // Source Y
$newWatermarkWidth, // Destination W
imagesy($watermark), // Destination H
imagesx($watermark), // Source W
imagesy($watermark)); // Source H

Categories