php combining text on images more than once? - php

I am trying to print users personal data like name, email, phone number on screen, using separate images, rather than printing out in clear text on page, thus getting cache possibly by Google. Trying to print like below:
Name - image with name text created on fly
Email - image with email text created on fly
Phoneno - image with number text created on fly
The code I have provided merges text into an image ie name, but only allows me to create one image to send back to browser, how can I try to get my script to send more than one image back to browser?
I have tried adding more parameters to my function function 'create_image($name,$email,$number)' but only prints one field to browser, perhaps something to do with header()?
<?php
//Send a generated image to the browser
$name="Bob";$email="bob#email.co.uk";$number="12345678901";
create_image($name);
function create_image($value)
{
//Set the image width and height
$width = 250;
$height = 20;
//Create the image resource
$image = ImageCreate($width, $height);
//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);
//Make the background black
ImageFill($image, 0, 0, $black);
//Add randomly generated string in white to the image
//imagestring ( resource $image , int $font-(font size) , int $x-(from left) , int $y-(from right) , string $string , int $color-(font-colour) )
ImageString($image, 5, 10, 3, $value, $white);
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
//Free up resources
ImageDestroy($image);
}
?>
Thanks for any replies

$name="Bob";$email="bob#email.co.uk";$number="12345678901";
switch ($_GET['option']) {
case 'name':
create_image($name);
break;
case 'email':
create_image($email);
break;
case 'number':
create_image($number);
break;
}
And call your script with scriptname.php?option=name

its not really a best practice, but I i would try to change your function, that it saves the images to a file, and returns the file path, when theres is a valid file.
So you don't have to create every call a new image (which is slow), and you get a valid filepath, which means you don't have to care about headers and so on

Related

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.

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.

Overlay a PNG image (made in photoshop with transparency) on to a JPG using PHP

I have two images. I need to place one png image with some transparent layers on a jpeg.
This code SAVES the image in the dir as "merged.png" instead of showing it -
<?php
header("Content-Type: image/png");
$width = 200;
$height = 200;
$base_image = imagecreatefromjpeg("image.jpg");
$top_image = imagecreatefrompng("miod.png");
$merged_image = "merged.png";
imagesavealpha($top_image, true);
imagealphablending($top_image, true);
imagecopy($base_image, $top_image, 100, 100, 100, 100, $width, $height);
imagepng($base_image, $merged_image);
?>
-How do I display it on the page itself?
imagepng has two main modes of operation. With a single parameter, it outputs the file directly, so it'll show up in the browser. With two parameters, as you have, it wills save the image from the first parameter into the file given in the second parameter.
So -- just drop your second parameter, and things should work fine:
imagepng($base_image);

Does html can be use with dynamic generated images in php?

I am using this code to create an image
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
(A)print ('<div class="test">');
imagepng($im);
print ('</div>');
(B)imagedestroy($im);
?>
The code work fines if i comment the line number 'A' and 'B' and it generates the image on the browser with testing written on it. But i want the image to be in a div. so i uncomment the line (A) and (B) but it is not giving right output. The generated html is also strange generated html is
<img src="http://localhost/php/test92.php" alt="The image “http://localhost/php/test92.php” cannot be displayed, because it contains errors.">
Basically, to create dynamic image in HTML, you will need 2 PHP files:
one for the image itself
another one for PHP to display it.
Let's take a look how to do it:
You create image.php that accept parameter, like: image ID or file name. For security reason, you HAVE to filter whatever parameter it get.
Why you have to do this? because, to generate image, you can't mix it with another HTML output. Let alone a single space or return as this will render the image broken.
You do the HTML thing on another PHP, say test92.php. To the HTML logic here, like:
get image data
loop the data
display image => <img src="image.php?imageID=12" alt="" />
If you want a div around your image you have to do that in the html, you can't do that in the image generation code
<div>
<img src="http://localhost/php/test92.php">
</div>
If you are getting errors regarding the image, try browsing the image url http://localhost/php/test92.php and see what it looks like.
Does it show an image like you are expecting?

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