Trying to render avatar image over a signature image with PHP - php

I am making a dynamic signature generator and I would like to add a avatar image in part of the signature. How would I do this as well as sizing down the image?

Here is an example.
I have 2 images:
https://s-media-cache-ak0.pinimg.com/736x/d1/2e/9e/d12e9ecd9f4c4e45dafa5880c7d99c73.jpg
http://cf.juggle-images.com/matte/white/280x280/php-1-logo-primary.jpg
Steps:
Set the content type header - in this case image/jpg
Create new canvas, width: 720, height: 637
Create two new images from file or URL, $icon1 & $icon2
Copy part of $icon1 to $canvas
Copy and resize part of $icon2 to $can
Output image to browser
<?php
header('Content-Type: image/jpg');
$canvas = imagecreatetruecolor(720, 637);
$icon1 = imagecreatefromjpeg('https://s-media-cache-ak0.pinimg.com/736x/d1/2e/9e/d12e9ecd9f4c4e45dafa5880c7d99c73.jpg');
$icon2 = imagecreatefromjpeg('http://cf.juggle-images.com/matte/white/280x280/php-1-logo-primary.jpg');
//add 2 source images
imagecopy($canvas, $icon1, 0, 0, 0, 0, 720, 637);
imagecopyresized($canvas, $icon2, 0, 0, 0, 0, 100, 100, 280, 280);
//Output image to browser
imagejpeg($canvas);
?>

You can do that by copying one image on the other, with some resampling (to make the avatar smaller, if desired.).
PHP must have GD libs included, if not, make sure you have support for GD, otherwise you cannot use the functions.
Check your GD support: function.gd-info.php
Then start reading here:
function.imagecopy.php
or here: function.imagecopyresampled.php

Related

Imagemagick, place a created image over another without saving

I want to stay away from using exec if possible. I have a jpg image on the server that never changes. I just want to place some text over the top of it but I want that text to be curved and virticle. So far I have created the virticle curved text with a transparent background. Now I just want to slide it over an image that is already made and sitting on the server. Is this possible to do with out saving the generated text image I have?
I have been playing with compositeImage but I am messing something up because the page fails to load with no error.
Here is what I have so far:
$phone='555-555-5555';
$draw = new ImagickDraw();
$draw->setFontSize(48);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);
$draw->setFillColor('#'.$color);
$textOnly = new Imagick();
$textOnly->newImage(900, 300, "none");
$textOnly->setImageFormat('png');
$textOnly->annotateImage($draw, 0, 100, 0, $phone);
$distort = array(270);
$textOnly->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$textOnly->setImageMatte(true);
$textOnly->distortImage(Imagick::DISTORTION_ARC, $distort, false);
$textOnly->setImagePage(0, 0, 0, 0);
$textOnly->setformat('png');
$textOnly->cropImage(170, 445, 45, 105);
header("Content-Type: image/png");
print $textOnly->getimageblob();
I have also tried the above code with this at the end but I get 500 errors:
$bgImage->readImage(BASE_PATH.'/src-images/background.jpg');
$bgImage->compositeImage($textOnly, Imagick::COMPOSITE_DEFAULT, 10, 20);
header("Content-Type: image/png");
echo $bgImage->getimageblob();
Here is the resulting image when I get it working with text only and without trying to add the second image:
This resolved it....
$bgImage = new Imagick
NOT
$bgImage->readImage

Crop an image and then save it in the same directory

I want to crop a part of an image of 100x100px for example from the middle to 20px height and 30px width and then save it in another file all with PHP.
I was reading and testing some code but i think im lost.
I want to do this because later i want to use OCR to get the text from the new img cropped.
Any help would be great!
Here is some code that i found in the documentation of php.net
<?php
// Create image instances
$src = imagecreatefrompng('waka.png');
$dest = "Select somehow /images ";
// Copy
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40);
// Output and free from memory
header('Content-Type: image/png');
imagepng($dest);
imagedestroy($dest);
imagedestroy($src);
?>
Im just getting some error about the img can't show when i run it in my local server.
I'm not really sure what i need to change to get a new png. Actually i have 2 img waka.png and wuku.png for testing.
Starting with your code, this one works for me:
<?php
// load your source image
$src = imagecreatefrompng('1.png');
// create an image resource of your expected size 30x20
$dest = imagecreatetruecolor(30, 20);
// Copy the image
imagecopy(
$dest,
$src,
0, // 0x of your destination
0, // 0y of your destination
50, // middle x of your source
50, // middle y of your source
30, // 30px of width
20 // 20px of height
);
// The second parameter should be the path of your destination
imagepng($dest, '2.png');
imagedestroy($dest);
imagedestroy($src);
You should have 2.png being your cropped image.

Add black bars to create a 16x9 image

I have a jpg on my server. I use
imagecreatefromjpeg($imgPath);
to open it. I want to make it a 16x9 image by adding black bars to either the top+bottom or left+right. (Think background-size: contain; background-position: center;) This is all I have:
$img_info = getimagesize($imgPath);
I know I need to use ImageCreateTrueColor to make the blank image, imagecopyresampled to create the image, and imagejpeg to save it. But I have no idea how to put them together. Thanks!
This will do the trick:
$im=imagecreatefromjpeg ($imgPath);
$width=ImageSX($im); $height=ImageSY($im); $ratio=16/9;
$width_out=$width; $height_out=$height;
if ($height_out*$ratio<$width_out) {$height_out=floor($width_out/$ratio);} else {$width_out=floor($height_out*$ratio);}
$left=round(($width_out-$width)/2);
$top=round(($height_out-$height)/2);
$image_out = imagecreatetruecolor($width_out,$height_out);
$bg_color = ImageColorAllocate ($image_out, 0, 0, 0);
imagefill($image_out,0,0,$bg_color);
imagecopy($image_out, $im, $left, $top, 0, 0, $width,$height);
imagejpeg($image_out);
How it works: you create the $im container, and check for width and height.
After this, the script checks which side is smaller than the other (multiplied / divided by the ratio) and adjust the output size.
Calculate where the original image should be placed (center alignment) by dividing the difference between the original and the output image dimensions by 2.
Copy over the original image at the given position
Output, done.

PHP function not displaying barcode using TrueType fonts

Hello I am using a function that I found in Internet to display a barCode using a TrueType font, here is the code:
//For displaying barcodes
//Arguments are:
// code Number you want outputted as a barcode
//You can use this script in two ways:
// From a webpage/PHP script <img src='/images/barcode.php?code=12345'/>
// Directly in your web browser http://www.example.com/images/barcode.php?code=12345
//Outputs the code as a barcode, surrounded by an asterisk (as per standard)
//Will only output numbers, text will appear as gaps
//Image width is dynamic, depending on how much data there is
header("Content-type: image/png");
$file = "barcode.png"; // path to base png image
$im = imagecreatefrompng($file); // open the blank image
$string = "123123123"; // get the code from URL
imagealphablending($im, true); // set alpha blending on
imagesavealpha($im, true); // save alphablending setting (important)
$black = imagecolorallocate($im, 0, 0, 0); // colour of barcode
$font_height=40; // barcode font size. anything smaller and it will appear jumbled and will not be able to be read by scanners
$newwidth=((strlen($string)*20)+41); // allocate width of barcode. each character is 20px across, plus add in the asterisk's
$thumb = imagecreatetruecolor($newwidth, 40); // generate a new image with correct dimensions
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, 40, 10, 10); // copy image to thumb
imagettftext($thumb, $font_height, 0, 1, 40, $black, 'B2FI25HRc.ttf', '*'.$string.'*'); // add text to image
//show the image
imagepng($thumb);
imagedestroy($thumb);
I cannot find the error why the function doesn't display the image. Any ideas? The font is in the same directory with the php function and I tried relative and absolute paths to the font with no results. Any suggestion?
Thank you very much
You need to check for error messages.
For debugging, comment out the header line and add these lines on the top to show all errors:
ini_set('display_errors',true);
error_reporting(E_ALL);
In many cases the error messages will tell you pretty clear whats wrong.

Add styled backgrounds to images in PHP

I am working on improving one of my Facebook apps by allowing the user to upload an image and apply a styled border, or frame to it (i.e. clouds, stars, sky etc). The user chould also be able to save the image, with the border after it has been applied. This explains a little better what I need:
http://zbrowntechnology.info/ImgDisp/imgdisp.php
If you have any other questions or need more details, please let me know.. I'll edit this post.
Use imagecopy(). The example on that page is done using the transparency option with imagecopymerge() but I don't think you need that.
Using imagecopy() you'll specify the X/Y coordinates to use for positioning:
imagecopy( $borderimage, $topimage, 20, 20, 0, 0, $width, $height);
Where $width and $height will be the entire width and height of the top image. You'll want to replace 20 and 20 with the measurement for how much of the border image will be showing around the borders. You will probably have to resize the top image to the exact dimensions you want, or else it might overlap the border a little too far to the right or bottom. (see imagecopyresampled())
Edit:
Here's a rough way to do the whole process (assuming chosenborder.png is the border they chose, and uploadedimage.png is the image they uploaded. If it's a different image type you'll use the corresponding function).
$borderx = 20; // The width of our border
$border = imagecreatefrompng("chosenborder.png");
$topimage = imagecreatefrompng("uploadedimage.png");
$bordersize = getimagesize($border);
$topimagesize = getimagesize($topimage);
/* The new dimensions of topimage. $borderx*2 means we account for
the border on both left and right, top and bottom. */
$newx = $bordersize[0] - ($borderx*2);
$newy = $bordersize[1] - ($borderx*2);
imagecopyresampled( $topimage_scaled, $topimage, 0, 0, 0, 0,
$newx, $newy, $topimagesize[0], $topimagesize[1]);
/* Merge the images */
imagecopy( $border, $topimage_scaled, $borderx, $borderx,
0, 0, $width, $height);
/* Output the image */
imagepng($border, "newimage.png");
/* Free up the memory occupied by the image resources */
imagedestroy($border);
imagedestroy($topimage);
After the user uploads their image, find chosenborder.png and uploadedimage.png, run the above script, then display newimage.png to the user and you're good to go. Just make sure you call imagedestroy() on the temporary image resources or they'll eat up memory.
If you don't want to keep the generated image on your server, you can omit the second argument to imagepng() which will make it send the image information directly as an image to the browser, in which case you'll want to write the correct image HTTP headers.
Client-side solution by using css3:
checkout the css3 property border-image
(dosen't meet the requirement of saving the img with the border)
Server-side solution by merging 2 different images:
<?php
$imgFile = 'img.jpg';
$brdFile = 'brd.jpg';
$img = addBorder($imgFile,$brdFile);
outputImage($img);
function addBorder($imgFile,$brdFile)
{
$img=imagecreatefromjpeg($imgFile);
$brd=imagecreatefromjpeg($brdFile);
$imgSize = getimagesize($imgFile);
$brdSize = getimagesize($brdFile);
//NOTE: the border img MUST be bigger then the src img
$dst_x = ceil(($brdSize[0] - $imgSize[0])/2);
$dst_y = ceil(($brdSize[1] - $imgSize[1])/2);
imagecopymerge ( $brd, $img, $dst_x, $dst_y, 0, 0, $imgSize[0], $imgSize[1] ,100 );
return $brd;
}
function outputImage($img)
{
header('Content-type: image/png');
imagepng($img);
}
?>

Categories