I need to place an image to the center of another image(both horizontally and vertically) with dimension 700*350. I'm trying with the following code. But I'm getting image as stretched.
#header("Content-Type: image/png");
$imageURL = "flower.jpg";
// create a transparent background image for placing the $imageURL image
$imageResource = imagecreatetruecolor(700, 350);
imagesavealpha($imageResource, true);
$transparentColor = imagecolorallocatealpha($imageResource, 0, 0, 0, 127);
imagefill($imageResource, 0, 0, $transparentColor);
$backgroundImage = imagecreatefromjpeg($imageURL);
list($width, $height) = getimagesize($imageURL);
imagecopyresampled($imageResource, $backgroundImage, 350, 175, 0, 0, 700, 350, $width, $height);
imagepng($imageResource, "newimage.jpg");
This is not centering the image and also the file flower.jpg is getting deleted when I run this code. What I'm doing wrong in this?
Can anyone please help me to fix this? Thanks in advance.
So you need something like this?
#header("Content-Type: image/png");
$imageURL = "flower.jpg";
// create a transparent background image for placing the $imageURL image
$imageResource = imagecreatetruecolor(700, 350);
imagesavealpha($imageResource, true);
$transparentColor = imagecolorallocatealpha($imageResource, 0, 0, 0, 127);
imagefill($imageResource, 0, 0, $transparentColor);
$backgroundImage = imagecreatefromjpeg($imageURL);
list($width, $height) = getimagesize($imageURL);
imagecopyresampled($imageResource, $backgroundImage, 175, 85, 0, 0, 350, 175, $width, $height);
imagepng($imageResource, "newimage.jpg");
imagedestroy($imageResource);
imagedestroy($backgroundImage);
You had specified the center of the destination image as the destination coordinates and the whole destination image size instead of needed dimentions of the center rectangle into which the source image would be resized.
Also you didn't do imagedestroy, which you totally should.
Related
I try to convert a partly transparent png to a jpg in php with gdlib. I found two snippets to help me with that, but both methods have the same problem: The half transparent colors are darker and do not look right. Here a enlarged sample from photoshop: left the png (with white in background instead of transparent), right the converted png to jpg with both snippets I used:
difference png (left) to jpg (right)
Here the original Png-File: golf.png
Any help would be really appreciated!
$input_file = "card/golf.png";
$output_file1 = "card/golf1.jpg";
$output_file2 = "card/golf2.jpg";
$image = imagecreatefrompng($input_file);
$bg = imagecreatetruecolor(imagesx($image), imagesy($image));
imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
imagealphablending($bg, TRUE);
imagecopy($bg, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
imagejpeg($bg, $output_file1, 100);
imagedestroy($bg);
imagedestroy($image);
list($width, $height) = getimagesize($input_file);
$image = imagecreatefrompng($input_file);
$output = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $width, $height, $white);
imagecopy($output, $image, 0, 0, 0, 0, $width, $height);
imagejpeg($output, $output_file2, 100);
imagedestroy($output);
You're suffering from quantization. JPEG does not handle this type of image well at all. If you want to lessen the color changes you need to adjust your quantization tables. If you use all 1s for the quantization tables you don't get the color changes.
I want to resize my images to a square. Say I want a squared image of 500x500 and I have an image of 300x600
I want to resize that image down to 200x500 and then add a white background to it to make it 500x500
I got something working good by doing this:
$TargetImage = imagecreatetruecolor(300, 600);
imagecopyresampled(
$TargetImage, $SourceImage,
0, 0,
0, 0,
300, 600,
500, 500
);
$final = imagecreatetruecolor(500, 500);
$bg_color = imagecolorallocate ($final, 255, 255, 255)
imagefill($final, 0, 0, $bg_color);
imagecopyresampled(
$final, $TargetImage,
0, 0,
($x_mid - (500/ 2)), ($y_mid - (500/ 2)),
500, 500,
500, 500
);
It's doing almost EVERYTHING right. The picture is centered and everything. Except the background is black and not white:/
Anyone know what I'm doing wrong?
I think this is what you want:
<?php
$square=500;
// Load up the original image
$src = imagecreatefrompng('original.png');
$w = imagesx($src); // image width
$h = imagesy($src); // image height
printf("Orig: %dx%d\n",$w,$h);
// Create output canvas and fill with white
$final = imagecreatetruecolor($square,$square);
$bg_color = imagecolorallocate ($final, 255, 255, 255);
imagefill($final, 0, 0, $bg_color);
// Check if portrait or landscape
if($h>=$w){
// Portrait, i.e. tall image
$newh=$square;
$neww=intval($square*$w/$h);
printf("New: %dx%d\n",$neww,$newh);
// Resize and composite original image onto output canvas
imagecopyresampled(
$final, $src,
intval(($square-$neww)/2),0,
0,0,
$neww, $newh,
$w, $h);
} else {
// Landscape, i.e. wide image
$neww=$square;
$newh=intval($square*$h/$w);
printf("New: %dx%d\n",$neww,$newh);
imagecopyresampled(
$final, $src,
0,intval(($square-$newh)/2),
0,0,
$neww, $newh,
$w, $h);
}
// Write result
imagepng($final,"result.png");
?>
Note also, that if you want to scale down 300x600 to fit in 500x500 whilst maintaining aspect ratio, you will get 250x500 not 200x500.
How can I place a transparent image to a background image using PHP?
I have here my code:
$width = 400;
$height = 400;
$base_image = imagecreatefrompng("img/frame1.png");
$top_image = imagecreatefrompng("randyorton.png");
$merged_image = "merged.png";
imagealphablending($base_image, true);
imagesavealpha($base_image, true);
imagealphablending($top_image, true);
imagesavealpha($top_image, true);
imagecopy($base_image, $top_image, 10080, 10080, 0, 0, $width, $height);
imagecopy($base_image, $top_image, 80, 80, 0, 0, $width, $height);
header('Content-Type: image/png');
imagepng($base_image, $merged_image);
What's wrong with this? The image I am creating now has a black background on it.
Please see this picture for reference:
Am I making sense?
Thanks!
I get it. I have a big width and height for the top image. I just get the size of the top image and pass it on the imagecopy function. :D
First: I want to create a PNG image and draw a shape inside it.
Second: Overlap the first PNG on another image (for example a jpg image)
The problem is: When I create the first PNG with a shape inside (the background is not transparent and is dark) so this make a black overlap on second image.
How can I fix it?
I don’t work so much with images function so I got trouble.
Notes: I need to create the first transparent PNG and then overlap it on second image. I don’t want to create shape directly on second image.
And The Code:
<?php
define('EXAMPLE_TMP_SERVERPATH', '');
define('EXAMPLE_TMP_URLRELPATH', '');
$tempDir = EXAMPLE_TMP_SERVERPATH;
$fileName = 'test3img.png';
$imgW = 125;
$imgH = 125;
# First
$base_image = imagecreatetruecolor($imgW, $imgH);
$black = imagecolorallocate($base_image, 0, 0, 0);
imagecolortransparent($base_image, $black);
$col[0] = imagecolortransparent($base_image, $black);
imagealphablending($base_image, true);
imagesavealpha($base_image, true);
imagefill($base_image, 0, 0, $col[0]);
imagefilledrectangle($base_image, 4, 4, 50, 25, 255);
imagepng($base_image, $tempDir.$fileName);
# First2
$target_image = imagecreatetruecolor($imgW*5, $imgH*5);
$black2 = imagecolorallocatealpha($base_image, 0, 0, 0, 127);
imagecolortransparent($target_image, $black2);
imagecopyresized($target_image, $base_image, 0, 0, 0, 0,$imgW, $imgH, $imgW, $imgH);
imagedestroy($base_image);
imagepng($target_image, $tempDir.$fileName);
imagedestroy($target_image);
# First2
# Second
$dest = imagecreatefromjpeg('../avatar.jpg');
$src = imagecreatefrompng(EXAMPLE_TMP_URLRELPATH.$fileName);
imagealphablending($dest, true);
imagesavealpha($dest, true);
imagealphablending($src, true);
imagecopyresampled(
$dest,
$src,
0,0,
0,0,
200, 200,
125, 125
);
imagepng($dest, EXAMPLE_TMP_URLRELPATH.'_m.jpeg');
imagedestroy($dest);
imagedestroy($src);
?>
Need to add the alpha channel too:
Alpha Channel
I need to change the color of the background of the ImageCreateTrueColor to white and then put an image on it
elseif(($height>50)&&($width<50))
{
$img_r = imagecreatefromjpeg($new_img_path);
$source = ImageCreateTrueColor(50, 50);
imagetruecolortopalette($source, FALSE, 2);
$bg = imagecolorat($source, 0, 0);
imagecolorset($source, $bg, 0, 0, 255);
// $white = imagecolorallocate($source,255,255,255);
// imagefilledrectangle($source, 0, 0, 50, 50, $white);
imagecopy($source, $img_r,0,0,0,0,$width,50);
header('Content-type: image/jpeg');
imagejpeg($source, $small_new_img_path);
here is the blue, but it doesn't matter, it doesn't put the image on the blue background
You want to create a png not a JPEG. Use imagepng and imagesavealpha.
See full example.