Adding one image at the bottom of another in PHP - php

I'd like to add one image to the bottom of another in php
I've this to load the images:
//load top
$top = #imagecreatefrompng($templateTop);
//load bottom
$bottom = #imagecreatefrompng($templateBottom);
Now I'd like to add them to one picture and display top and bottom together.
What way can I do this?
Thanks!

Use imagecopy:
$top_file = 'image1.png';
$bottom_file = 'image2.png';
$top = imagecreatefrompng($top_file);
$bottom = imagecreatefrompng($bottom_file);
// get current width/height
list($top_width, $top_height) = getimagesize($top_file);
list($bottom_width, $bottom_height) = getimagesize($bottom_file);
// compute new width/height
$new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
$new_height = $top_height + $bottom_height;
// create new image and merge
$new = imagecreate($new_width, $new_height);
imagecopy($new, $top, 0, 0, 0, 0, $top_width, $top_height);
imagecopy($new, $bottom, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);
// save to file
imagepng($new, 'merged_image.png');

To achieve this you would have to
a) Combine the image and store the result in a file
b) generate a suitable tag to point to it.
c) Avoid using that filename again, until that person had left.
If you want to combine two images just once, then use image magic.
If you frequently want to display two images one under the other, do so using suitable html, and let the browser do it.
E.g. Put the images in a
<div><div><img.../></div><div><img .../></div></div>
which you generate with php in the normal way.
(Which is easier than getting tags to appear here :)

$photo_to_paste = "photo_to_paste.png";
$white_image = "white_image.png";
$im = imagecreatefrompng($white_image);
$im2 = imagecreatefrompng($photo_to_paste);
// Place "photo_to_paste.png" on "white_image.png"
imagecopy($im, $im2, 20, 10, 0, 0, imagesx($im2), imagesy($im2));
// Save output image.
imagepng($im, "output.png", 0);

Related

GD adding border to image not working

So I have PHP class and I'm trying to add white border with spacing around the uploaded image:
private function __draw_white_border()
{
// get source image and dimensions.
$src = $this->file_data['im'];
$src_w = imagesx($src);
$src_h = imagesy($src);
// create destination image with dimensions increased from $src for borders.
$dest_w = $src_w + 10;
$dest_h = $src_h + 10;
$dest = imagecreatetruecolor($dest_w, $dest_h);
// draw white border (no need for black since new images default to that).
imagerectangle($dest, 1, 1, $dest_w - 2, $dest_h - 2, 0x00ffffff);
imagerectangle($dest, 0, 0, $dest_w - 1, $dest_h - 1, 0x00ffffff);
// copy source image into destination image.
imagecopy($dest, $src, 5, 5, 0, 0, $src_w, $src_h);
}
this function works good when not in the class but with this class its not working. It seems that this function don't save image or something, I dont know.. It should load image from $this->file_data['im'] i think and save to the same destination for further actions. Where is the problem here?
you need to create 2 images and layer the main image on top of the slightly larger second image. This way the larger image will be seen around the edge of the main image making it look like a border.
http://php.net/manual/en/function.imagecopy.php
I use this to layer the images correctly. If the destination image is larger lets say 100x100px. The source image is 80x80px. You will add 10 to the x and y of the source image which will place the source image in the center of the 100x100px image. If the 100x100px image is a plain black image it will generate a clean black border
You are not putting the amended image back anywhere where you can get to it again after this method is run. This code (see last line) puts it back where the original image came from.
private function __draw_white_border()
{
// get source image and dimensions.
$src = $this->file_data['im'];
$src_w = imagesx($src);
$src_h = imagesy($src);
// create destination image with dimensions increased from $src for borders.
$dest_w = $src_w + 10;
$dest_h = $src_h + 10;
$dest = imagecreatetruecolor($dest_w, $dest_h);
// draw white border (no need for black since new images default to that).
imagerectangle($dest, 1, 1, $dest_w - 2, $dest_h - 2, 0x00ffffff);
imagerectangle($dest, 0, 0, $dest_w - 1, $dest_h - 1, 0x00ffffff);
// copy source image into destination image.
imagecopy($dest, $src, 5, 5, 0, 0, $src_w, $src_h);
$this->file_data['im'] = $dest;
}

How to merge images in PHP?

I have two images which are loaded from an external URL. I want to place one image on top of another (keeping the size). I actually don't know the sizes of the images as they are from an external URL and are different each time.
So I want a function which can be called like merge($url1,$url2,$final_name). I have googled on this but none worked.
I want this 3.png :
--------------------------------------------------------------------------
|
IMAGE 1 |
|
|
___________________________________________________________________________
|
IMAGE 2 |
|
|
___________________________________________________________________________
$top_file = 'image1.png';
$bottom_file = 'image2.png';
$top = imagecreatefrompng($top_file);
$bottom = imagecreatefrompng($bottom_file);
// get current width/height
list($top_width, $top_height) = getimagesize($top_file);
list($bottom_width, $bottom_height) = getimagesize($bottom_file);
// compute new width/height
$new_width = ($top_width > $bottom_width) ? $top_width : $bottom_width;
$new_height = $top_height + $bottom_height;
// create new image and merge
$new = imagecreate($new_width, $new_height);
imagecopy($new, $top, 0, 0, 0, 0, $top_width, $top_height);
imagecopy($new, $bottom, 0, $top_height+1, 0, 0, $bottom_width, $bottom_height);
// save to file
imagepng($new, 'merged_image.png');
This does the job but the merged image loses its color and becomes nearly black and white like this:
Instead of this:
// create new image and merge
$new = imagecreate($new_width, $new_height);
I used:
// create new image and merge
$new = imagecreatetruecolor($new_width, $new_height);
It worked!
Thanks!
i looked up ur code but i would save the folder in something like that:
$time = date('d-M-Y') . '-TIME-' . date('H-i-s');
$save_finalimage_socialmedia = public_path("storage/image-{$time}.jpeg");
imagejpeg($new, $save_finalimage_socialmedia);
PS: i did it in the jpeg format, so if you want, you can change in ur format (png,jpg,...)
PS: #user4273356 ur code worked for me

Cannot get image to save in desired directory using imagepng()

I am converting text to image using PHP's imagettftext function and then saving it to the server. The problem I'm having is getting it to save in the directory that I want, it has no problems saving to the current directory where the script runs.
Here is my code:
function txt2img($text, $fSize)
{
// Path to our font file
$fType = 'verdana.ttf';
// First we create our bounding box for the text
$bbox = imagettfbbox($fSize, 0, $fType, $text);
// We calculate the width and height using the x and y points.
// Width is: upper right corner, X position - upper left corner, X position
$width = $bbox[4] - $bbox[6] + 8;
// Height is: lower left corner, Y position - upper left corner, Y position
$height = $bbox[1] - $bbox[7] + 4;
// Determine the x position for the text to start
$x = 2;
// Determine the y position for the text to start
$y = $height - 4;
// Create the image
$im = imagecreatetruecolor($width, $height);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
// Create the background color to match the website
$default = imagecolorallocate($im, 255, 238, 198);
// Set the background color
imagefilledrectangle($im, 0, 0, $width, $height, $default);
// Write it
imagettftext($im, $fSize, 0, $x, $y, $black, $fType, $text);
// Set the location to save the image.
//$save = "email_address.png";
$save = "../images/contact/email_address.png";
// Set the folder permissions to allow us to write to the folder.
chmod("../images/contact/",0755);
// .png tends to look better than a .jpg
imagepng($im,$save);
imagedestroy($im);
// Return the image dimensions so they can be stored in the database. This is used to properly diplay the image on the front end.
return ($width."+".$height);
}
If I use the $save location that is commented out, it works perfectly fine, it just isn't in the directory that I want it to be in. I have tried other paths, such as "/images/contact/email_address.png" but no joy. This also worked without the "chmod" permission change, I just added it as another option for troubleshooting. I have also gone directly to the folder and set the permissions for the user, again, no joy.
I'm running this on a local WAMP server. Also, $text has been checked and $fSize is the font size, which has also been checked. Because this works so well in the current directory, I feel this is not a coding issue as much as it is a directory issue, but that's just my view.
So it does save in the folder you're working in as per the commented out save. Have you tried images/contact/email_address.png"; and /images/contact/email_address.png"; without the dots ? The dots are going up a level and if that's above the root it will be unaccesable to php

PHP GD poor thumb quality

$tmp_im = imagecreatetruecolor($width, $height);
$x = $this->getX();
$y = $this->getY();
$w = floor($resize_height * ($x / $y));
$h = $resize_height;
$this->tmp_im = imagecreatetruecolor($w, $h);
imagealphablending($this->tmp_im, false);
imagesavealpha($this->tmp_im, true);
imagecopyresampled($this->tmp_im, $this->im, 0, 0, 0, 0, $w, $h, $this->getX(), $this->getY());
$this->im = $this->tmp_im;
$hs = floor(($width - $this->getX())/2);
$vs = floor(($height - $this->getY())/2);
imagecopy($tmp_im, $this->im, $hs, $vs, 0, 0, $this->getX(), $this->getY());
$this->im = $tmp_im;
the results is a poor quality resized image, what im doing wrong? i also tried to use a imagejpeg with quality = 100
Use imagecopytruecolor() instead of
imagecopy($tmp_im, $this->im, $hs, $vs, 0, 0, $this->getX(), $this->getY());
This is a Notice on the official PHP documentation page:
There is a problem due to palette image limitations (255+1 colors).
Resampling or filtering an image commonly needs more colors than 255,
a kind of approximation is used to calculate the new resampled pixel
and its color. With a palette image we try to allocate a new color, if
that failed, we choose the closest (in theory) computed color. This is
not always the closest visual color. That may produce a weird result,
like blank (or visually blank) images. To skip this problem, please
use a truecolor image as a destination image, such as one created by
imagecreatetruecolor().

Create a picture with GD containing other images

I would like to create a picture in PHP with GD composed by different other pictures. For example I have 6 pictures (or more) and I would like to create ONE picture who contain these different pictures.
The Difficulty is that my final picture must have a fixed width and height (304x179), so if the different pictures are too big they must be cut. This is an example from IconFinder :
This picture is composed by 6 images, but the 3rd bird (green) is cutted, and the 4, 5 and 6 are cutted in the bottom. This is what I want, can you give me some help to write this code in PHP ?
Thanks
Create your primary image and consider it your "canvas."
From there, use imagecopy() to copy the smaller images into the canvas image.
See this for example:
<?php
header('Content-Type: image/jpg');
$canvas = imagecreatetruecolor(304, 179);
$icon1 = imagecreatefromjpeg('icon.jpg');
$icon2 = imagecreatefromjpeg('icon2.jpg');
// ... add more source images as needed
imagecopy($canvas, $icon1, 275, 102, 0, 0, 100, 100);
imagecopy($canvas, $icon2, 0, 120, 0, 0, 100, 100);
// ... copy additional source images to the canvas as needed
imagejpeg($canvas);
?>
In my example, icon.jpg is a 100x100 image which I am placing in the canvas such that its top left corner is located at 275, 102 in the canvas, which cuts off the right side.
Edit
I adjusted the code to be more similar to what you're doing.
Here a none tested modify spinet from one of my scripts, hope it can be usefull:
header('Content-type: image/png');
$image = array() //Populate this array with the image paths
//Create the Letters Image Objects
foreach($image as $a){
$image['obj'][] = imageCreateFromPNG($a);
}unset($a);
$canvasW = 300;
$canvasH = 300;
//Create Canvas
$photoImage = imagecreatetruecolor($canvasW,$canvasH);
imagesavealpha($photoImage, true);
$trans_color = imagecolorallocatealpha($photoImage, 0, 0, 0, 127);
imagefill($photoImage, 0, 0, $trans_color);
//Merge Images
$Offset_y = 0;
$images_by_row = 3;
$images_rows_height = 100; // height of each image row
$counter = 0;
foreach($image['obj'] as $a){
$counter++;
$width = ceil(imagesx($a));
$height = ceil(imagesy($a));
if(!isset($offset)){ $offset = 1; }
imageComposeAlpha($photoImage, $a, $offset, $Offset_y,$width,$height);
if($offset >= 1){
$offset = $offset + $width;
}
//Check if new row next time
if($counter >= $images_by_row){
if($images_by_row%$counter){
$offset_y += $images_rows_height;
}
}
}unset($a);
imagepng($photoImage);

Categories