Improving Performance on this Image Creation function - php

I am making use of GD2 and the image functions to take in a string and then convert that into an image using different fonts at different sizes. The function I use is below.
Currently, its pretty quick but not quick enough. The function gets called about 20 times per user and the images generated are always new ones (different) so caching isn't going to help!
I was hoping to get some ideas on how to make this function faster. Maybe supply more RAM to the script running? Anything else that is specific to this PHP function?
Anything else that I can do to tweak performance of this function?
function generate_image($save_path, $text, $font_path, $font_size){
$font = $font_path;
/*
* I have simplifed the line below, its actually a function that works out the size of the box
* that is need for each image as the image size is different based on font type, font size etc
*/
$measure = array('width' => 300, 'height'=> 120);
if($measure['width'] > 900){ $measure['width'] = 900; }
$im = imagecreatetruecolor($measure['width'], $measure['height']);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, $measure['width'], $measure['height'], $white);
imagettftext($im, $font_size, 0, $measure['left'], $measure['top'], $black, $font, ' '.$text);
if(imagepng($im, $save_path)){
$status = true;
}else{
$status = false;
}
imagedestroy($im);
return $status;
}
Thanks all for any help

I think its good

Instead of creating a new image each time, you could have a blank PNG file (we already know that the maximum width is 900px, do you have a fixed maximum height you can use?), open it, add your text, and then crop it (see imagecopy()).
I'm not sure, but it might be faster than what you are currently doing.

Related

Creating image with fix background and randomly displayed text from a pre-defined list

I would like to create a script which is generating an image with one fix background image but display a randomly selected sentence on the image from a list which is previously given in the script.
So far I have a base and I've tried many variations for the random text, but none of them worked until this time.
Here is the script itself:
<?php
header('Content-type: image/png');
$text = 'The text which will be displayed';
$background_path = 'assets/bg_image.jpg';
$image = imagecreatetruecolor(1280, 720);
$background = imagecreatefromjpeg($background_path);
$color = imagecolorallocate($image, 255, 255, 255);
$title_sizes = imagettfbbox(24, 0, 'assets/roboto-medium.ttf', $text);
$title_height = $title_sizes[3];
$title_width = $title_sizes[4];
imagecopy($image, $background, 0, 0, 0, 0, 1280, 720);
imagettftext($image, 24, 0, (640 - $title_width / 2), (360 - $title_height / 2), $color, 'assets/roboto-medium.ttf', $text);
imagepng($image);
imagedestroy($image);
imagedestroy($background);
?>
So the thing is that the line "$text" allows only one sentence to be displayed. I would like to have multiple lines (various sentences) instead which will be displayed on the given background image randomly upon refresh.
Can you please help me to solve this problem?
Thank you very much in advance!
Regards,
Adam

"z-index" in php GD

In css, we have a property called "z-index", what is the same in PHP GD and Image Functions to control the "z-index"?
I've been searching but I can't find one, please help.
Thankyou
there nothing existing like z-index in php GD library
but there several ways to overlap image over image or text over image
$redimg = imagecreatetruecolor(100, 100);
$image = imagecreatefrompng('image.png');
// sets background to red
$red = imagecolorallocate($redimg, 255, 0, 0);
imagefill($redimg, 0, 0, $red);
// Merge the red image onto the PNG image
imagecopymerge($image, $redimg, 0, 0, 0, 0, 100, 100, 75);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
imagedestroy($redimg);
here one example, or let me know, what exactly you trying to do, I will help you
There's more information here.

Fit the text into image PHP GD

Helllo, Using GD I'm creating a png image from a $text. The $text is script generated and may take a few lines.
So, here is my code:
$imageWidth=400;
$imageHeight=100;
$logoimg = imagecreatetruecolor($imageWidth, $imageHeight); //create Image
imagealphablending($logoimg, false);
imagesavealpha($logoimg, true);
$col=imagecolorallocatealpha($logoimg,255,255,255,127);
imagefill($logoimg, 0, 0, $col);
$white = imagecolorallocate($logoimg, 0, 0, 0);
$font = "TNR.ttf"; //font path
$fontsize=14;
$x=10;
$y=20;
$angle=0;
imagettftext($logoimg, $fontsize,$angle , $x, $y, $white, $font, $text);
$target="temp.png"; //path of target
imagepng($logoimg,$target);
My problem is that sometimes the text goes cropped by a few pixels. Here is an example:
So, the question is, how can I make the text fit the image? Thank you for your time!
You may use imagettfbox function to check edges before writing the text:
http://www.php.net/manual/en/function.imagettfbbox.php
Or, perhaps, add a border around text.

GD Captcha security form coming up blank

So I'm trying to make a Captcha form for my registration form and I'm not sure what's going wrong because whenever I open up the page I see the image shown when the url of an image is not found (I would post a picture, but I can't since this is my first question).
I'm not sure what I'm doing wrong because I was following a tutorial as I was making it and it seemed to work for them. Also, no errors are being displayed so I have no idea what could be wrong. My code is here:
<?php
session_start();
header('Content-type: image/jpeg');
$text = 4539;
$font_size = 4;
$image_height = 40;
$image_width = 200;
$image = imagecreate($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$font_colour = imagecolorallocate($image, 0, 0, 0);
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));
// Name the font to be used (note the lack of the .ttf extension)
$font = 'font';
imagettftext($image, 0, 15, 30, $font_colour, $font, $text);
?>
Also the $text variable will be set to random when I know the code works, the value is static for testing purposes. I would appreciate any help I can get. Thanks in advance
You need to output the image to the browser after you add the text to it:
imagettftext($image, 0, 15, 30, $font_colour, $font, $text);
imagepng($image);
imagedestroy($image);
The destroy may not be strictly needed as the php process exits after the script is done, but the examples have it so I included it.
Also, you can choose between png, gif and jpeg image formats by using either imagepng(), imagegif() or imagejpeg(). The png format should be good here.

imagettfbbox & imagettftext - resize,position,rotate

Ok, now it's officialy 10 hours that I'm trying to make this work, but by every minute I'm farther and farther from the final goal...
The thing is, I need to create a function, class or whatever that by the selected TTF or truetype font create, properly resize, and fill the image with a text given by the url. The image shouldn't be longer than that text.
I've given up on all my codes, and the best thing I've got so far is taken from the net ...
<?php
if(!isset($_GET['size'])) $_GET['size'] = 20;
if(!isset($_GET['text'])) $_GET['text'] = "Moj tekst";
$get_font = ( isset( $_GET['font'] ) ) ? $_GET['font'] : 'arial';
$size = imagettfbbox($_GET['size'], 0, "font/".$get_font.".ttf", $_GET['text']);
$xsize = abs($size[0]) + abs($size[2]);
$ysize = abs($size[5]) + abs($size[1]);
$image = imagecreate($xsize, $ysize);
imageSaveAlpha($image, true);
ImageAlphaBlending($image, false);
$transparentColor = imagecolorallocatealpha($image, 200, 200, 200, 127);
Imagefill($image, 0, 0, $transparentColor);
$blue = imagecolorallocate($image, 0, 0, 255);
$white = ImageColorAllocate($image, 255,255,255);
$black = ImageColorAllocate($image, 0,0,0); //-($_GET['size']/20)
imagettftext($image, $_GET['size'], 0, abs($size[0]), abs($size[5]), $black, "font/".$get_font.".ttf", $_GET['text']);
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
This code is ok, although the edge of the last or first latter is sometimes partly hidden or just it doesn't fit into the created image (depending on font size), which isn't the problem with smaller font size. And the function i really need is by changing image's rotation to change the entire images proportions so the text fits in.
Example : http://img199.imageshack.us/img199/9368/32739521.jpg
Also, if there is finished function or class that does this, it would be great.. :'( :)
If you are always missing one letter of the end of the beginning, why not just add the width of a single letter to your final size?
In my experience, imagettfbbox doesn't do a very good job of deciding what does and doesn't fit within a bounding box - you need to fudge it a little yourself :)
Hope this helps.
Luke Peterson

Categories