I am trying to create a photo frame where there will be a light red shade and a text aligned center. I am taking the actual size of the images and then trying to create the new image programmatically with the frame. Below is a sample that I did so far:
In the image, there is a white border that I've used for demonstration purpose. I want the light red shade should not go over the white border and the text should be exactly in the middle for every image. As I am working with the actual image size, the shade and text does not remain in the specific position. In some images, the light red shade and text does not show up. I did the following code to make it work but seem like missing something:
$size = getimagesize($file_tmp); //Gets the image file size
$width = $size[0];
$height = $size[1];
$images_orig = imagecreatefromjpeg($file_tmp); //Creates jpeg image from tmp file location
$photoX = imagesx($images_orig);
$photoY = imagesy($images_orig);
$images_fin = imageCreateTrueColor($width, $height);
$color = imagecolorallocate($images_fin, 255, 255, 255);
$lightRed = imagecolorallocatealpha($images_fin, 255, 0, 0, 100);
$black = imagecolorallocate($images_fin, 0, 0, 0);
$text = 'I am from China';
$font = 'MyriadPro-Light.ttf';
$positions_redline = ($height / 4)*3;
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width, $height, $photoX, $photoY);
Imagefilledrectangle($images_fin, 0, $positions_redline, $width, $height, $lightRed);
$font_size = 10;
/*Starts - This is what I tried to fit the text into the image specifically in the center*/
$bbox = imagettfbbox($font_size, 0, $font, $text);
$tbwidth = $bbox[2];
$factor = round(($width / $tbwidth), 0, PHP_ROUND_HALF_DOWN);
$newFontSize = $font_size * $factor;
/*Finishes - This is what I tried to fit the text into the image specifically in the center*/
print_r($bbox[2]);
print_r("<br/>".$factor);
// Get your Text Width and Height
$text_width = $bbox[2] - $bbox[6];
$text_height = $bbox[3] - $bbox[7];
// Calculate coordinates of the text
$x = ($photoX / 2) - ($text_width / 2);
$y = (($height - $positions_redline) / 2) - ($text_height / 2);
Imagettftext($images_fin, $newFontSize, 0, $x, $y + $positions_redline, $color, $font , $text); //Trying to write text and align it center here
$new_images = 'testImageResult.jpg';
ImageJPEG($images_fin,"Images/".$new_images);
Try this source
$file_tmp = '/var/www/html/testGuzzle/testImage.jpg';
$size = getimagesize($file_tmp); //Gets the image file size
$width = $size[0];
$height = $size[1];
$images_orig = imagecreatefromjpeg($file_tmp); //Creates jpeg image from tmp file location
$photoX = imagesx($images_orig);
$photoY = imagesy($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
$color = imagecolorallocate($images_fin, 255, 255, 255);
$lightRed = imagecolorallocatealpha($images_fin, 255, 0, 0, 100);
$black = imagecolorallocate($images_fin, 0, 0, 0);
$text = 'Im from VietNam';
$font = '/var/www/html/testGuzzle/OpenSans-SemiboldItalic.ttf';
$positions_redline = ($height/4)*3;
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width, $height, $photoX, $photoY);
Imagefilledrectangle($images_fin, 0, $positions_redline, $width, $height, $lightRed);
$font_size = 20;
$bbox = imagettfbbox($font_size, 0, $font, $text);
print_r($bbox);
// Get your Text Width and Height
$text_width = $bbox[2]-$bbox[0];
$text_height = $bbox[7]-$bbox[1];
// Calculate coordinates of the text
$x = ($photoX/2) - ($text_width/2);
$y = ($height-$positions_redline)/2) - ($text_height/2);
Imagettftext($images_fin, 40, 0, $x, $y+$positions_redline, $color, $font , $text); //Trying to write text and align it center here
$new_images = 'testImageResult.jpg';
ImageJPEG($images_fin,"/var/www/html/testGuzzle/".$new_images);
Related
The PHP code below is supposed to center the letter "M" vertically and horizontally. Vertically it is perfectly centered, but horizontally it is off.
What is really strange is that the offset appears correct, but it seems to be placed at a different coordinate then what I am telling it. (Correct starting X offset should be 10, but it appears to be placed after 16 pixels in).
<?php
header('Content-Type: image/png');
$image = imagecreatetruecolor(100, 400);
$font_size = 92;
$font = "RobotoCondensed-Bold.ttf"; // FILE PATH TO FONT
$angle = 0;
$text = "M";
$red = imagecolorallocate($image, 255, 0, 0);
$white = imagecolorallocate($image, 255, 255, 255);
// Get image dimensions
$image_width = imagesx($image);
$image_height = imagesy($image);
$text_bound = imageftbbox($font_size, $angle, $font, $text);
//Get the text upper, lower, left and right corner bounds
$lower_left_x = $text_bound[0];
$lower_left_y = $text_bound[1];
$lower_right_x = $text_bound[2];
$lower_right_y = $text_bound[3];
//$upper_right_x = $text_bound[4];
$upper_right_y = $text_bound[5];
//$upper_left_x = $text_bound[6];
//$upper_left_y = $text_bound[7];
//Get text Width and text height
$text_width = $lower_right_x - $lower_left_x; //or $upper_right_x - $upper_left_x
$text_height = $lower_right_y - $upper_right_y; //or $lower_left_y - $upper_left_y
//Get the starting position for centering
$start_x_offset = ($image_width - $text_width) / 2;
$start_y_offset = ($image_height + $text_height) / 2;
// Add text to image
imagettftext($image, $font_size, $angle, $start_x_offset, $start_y_offset, $red, $font, $text);
// Debug stats
$debug_text = "text_width:[" . $text_width . "]\n";
$debug_text .= "text_height:[" . $text_height . "]\n";
$debug_text .= "start_x_offset:[" . $start_x_offset . "]\n";
$debug_text .= "start_y_offset:[" . $start_y_offset . "]\n";
imagettftext($image, 8, 0, 0, 300, $white, $font, $debug_text);
imagepng($image);
imagedestroy($image);
?>
I have a script that is writing a text over an image.
It works fine, but the text is aligned to the left.
I am trying to center the text horizontally, but not sure how can I do that.
Here is the code that I have at the moment
//content type
header('Content-Type: image/png');
if (isset($_GET['text'])){$text = $_GET['text'];}else{$text = "";}
//font
$font = 'Ubuntu-Medium.ttf';
//font size
$font_size = 18;
//image width
$width = 400;
//text margin
$margin = 90;
//explode text by words
$text_a = explode(' ', $text);
$text_new = '';
foreach($text_a as $word){
//Create a new text, add the word, and calculate the parameters of the text
$box = imagettfbbox($font_size, 0, $font, $text_new.' '.$word);
//if the line fits to the specified width, then add the word with a space, if not then add word with new line
if($box[2] > $width - $margin*2){
$text_new .= "\n".$word;
} else {
$text_new .= " ".$word;
}
}
//trip spaces
$text_new = trim($text_new);
//new text box parameters
$box = imagettfbbox($font_size, 0, $font, $text_new);
//new text height
$height = $box[1] + $font_size + $margin * 2;
//create image
$im = imagecreatefromjpeg('source.jpg');
//create colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 51, 95, 32);
//color image
imagefilledrectangle($im, 0, 0, $width, $black);
// determine the size of the text so we can center it
$box = imagettfbbox($font_size, 0, $font, $text_new);
$text_width = abs($box[2]) - abs($box[0]);
$text_height = abs($box[5]) - abs($box[3]);
$image_width = imagesx($im);
$image_height = imagesy($im);
$x = ($image_width - $text_width) / 2;
$y = ($image_height + $text_height) / 2;
//add text to image
imagettftext($im, $font_size, 0, $x, $y, $black, $font, $text_new);
//return image
imagepng($im);
//frees any memory associated with image
imagedestroy($im);
Any ideas on how can I center the text?
I already centered the box that the text is placed, so the box is in the center of the image. The problem is that the text is aligned left and I need it in the center.
I have one problem with PHP create image. What is wrong with my following code?
<?php
$filename = 'play';
$img = "http://www.slcentral.com/ipod-mp3-player/5.JPG";
$image = imagecreatefromjpeg($img);
$cleft = 0;
$ctop = 45;
$canvas = imagecreatetruecolor(480, 270);
imagecopy($canvas, $image, 0, 0, $cleft, $ctop, 480, 360);
$image = $canvas;
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
// ADD THE PLAY ICON
$play_icon = "f-play.png";
$logoImage = imagecreatefrompng($play_icon);
imagealphablending($logoImage, TRUE);
$logoWidth = imagesx($logoImage);
$logoHeight = imagesy($logoImage);
// CENTER PLAY ICON
$left = round($imageWidth / 2) - round($logoWidth / 2);
$top = round($imageHeight / 2) - round($logoHeight / 2);
// CONVERT TO PNG SO WE CAN GET THAT PLAY BUTTON ON THERE
imagecopy( $image, $logoImage, $left, $top, 0, 0, $logoWidth, $logoHeight);
imagepng( $image, $filename .".png", 9);
// MASHUP FINAL IMAGE AS A JPEG
$input = imagecreatefrompng($filename .".png");
$output = imagecreatetruecolor($imageWidth, $imageHeight);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $imageWidth, $imageHeight, $white);
imagecopy($output, $input, 0, 0, 0, 0, $imageWidth, $imageHeight);
// OUTPUT TO 'i' FOLDER
header("Content-type: image/jpeg");
imagejpeg($output, $filename . ".jpg", 95);
// UNLINK PNG VERSION
#unlink($filename .".png");
die();
?>
Above code is create one image from giving url and add play icon to it. And I want to access to http://coolrss.com/create-img.php it will show image with play icon.
Please help!
Make sure the path has the right permission or you will get:
PHP Warning: imagepng(): Unable to open 'play.png' for writing: Permission denied in /var/www/coolrss/public_html/create-img.php on line 32
Make sure you have installed GD.
Start your code with this header (after the php tag (<?php))
header("Content-Type: image/jpeg");
At the end (last line) add
imagejpeg($output, NULL, 100);
Your code looks like this now:
<?php
header("Content-Type: image/jpeg");
$filename = 'play';
$img = "http://www.slcentral.com/ipod-mp3-player/5.JPG";
$image = imagecreatefromjpeg($img);
$cleft = 0;
$ctop = 45;
$canvas = imagecreatetruecolor(480, 270);
imagecopy($canvas, $image, 0, 0, $cleft, $ctop, 480, 360);
$image = $canvas;
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
// ADD THE PLAY ICON
$play_icon = "f-play.png";
$logoImage = imagecreatefrompng($play_icon);
imagealphablending($logoImage, TRUE);
$logoWidth = imagesx($logoImage);
$logoHeight = imagesy($logoImage);
// CENTER PLAY ICON
$left = round($imageWidth / 2) - round($logoWidth / 2);
$top = round($imageHeight / 2) - round($logoHeight / 2);
// CONVERT TO PNG SO WE CAN GET THAT PLAY BUTTON ON THERE
imagecopy( $image, $logoImage, $left, $top, 0, 0, $logoWidth, $logoHeight);
imagepng( $image, $filename .".png", 9);
// MASHUP FINAL IMAGE AS A JPEG
$input = imagecreatefrompng($filename .".png");
$output = imagecreatetruecolor($imageWidth, $imageHeight);
$white = imagecolorallocate($output, 255, 255, 255);
imagefilledrectangle($output, 0, 0, $imageWidth, $imageHeight, $white);
imagecopy($output, $input, 0, 0, 0, 0, $imageWidth, $imageHeight);
imagejpeg($output, NULL, 100);
Screenshot - Works for me
I'm trying to create an image with a watermark text in it and I want it to be central on the image. The text I want to watermark can be anywhere from 5 characters to 15 characters, therefore I cannot put a final size for the text to fit every image.
This is the code I use to create the watermarked image
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$font = '../fonts/proxima-nova-light.otf';
$font_size = 100;
imagettftext($image_p, $font_size, 0, 303, 190, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
Which does an excellent job on some texts but when I try it on longer texts it looks bad.
I want to - somehow - calculate the optimal size of text and choose size, x and y from there.
Any ideas?
After doing some research with help from #Sammitch I was able to figure it out. Here is the working code:
<?php
function watermarkImage ($SourceFile, $WaterMarkText, $DestinationFile) {
$font = 'fonts/your-font-here.ttf';
$font_size = 40;
list($width, $height) = getimagesize($SourceFile);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($SourceFile);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$bbox = imagettfbbox($font_size, 0, $font, $WaterMarkText);
$x = $bbox[0] + (imagesx($image) / 2) - ($bbox[4] / 2) + 10;
$y = $bbox[1] + (imagesy($image) / 2) - ($bbox[5] / 2) - 5;
imagettftext($image_p, $font_size, 0, $x, $y, $black, $font, $WaterMarkText);
if ($DestinationFile<>'') {
imagejpeg ($image_p, $DestinationFile, 100);
} else {
header('Content-Type: image/jpeg');
imagejpeg($image_p, null, 100);
};
imagedestroy($image);
imagedestroy($image_p);
};
?>
Calculate the position of the watermark in the output image (the
watermark shall be placed in the Center of the Image)
$watermark_pos_x = (imagesx($image)/2) - (imagesx($watermark)/2) - 15;
$watermark_pos_y = (imagesy($image)/2) - (imagesy($watermark)/2) - 10;
Use this value on function.
So I have an image, and I am writing text and a color box onto the image. It works but it's being added to the image in the top right corner, but I need it in the center of the image. I tried changing the x and y variables, but it only moves the text and not the white box.
Here is code
$image_filepath = './kenshin.jpg';
saveImageWithText("Welcome to Eureka!", $color, $image_filepath);
function saveImageWithText($text, $color, $source_file) {
$public_file_path = '.';
// Copy and resample the imag
list($width, $height) = getimagesize($source_file);
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($source_file);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width, $height);
// Prepare font size and colors
$text_color = imagecolorallocate($image_p, 0, 0, 0);
$bg_color = imagecolorallocate($image_p, 255, 255, 255);
$font = $public_file_path . '/arial.ttf';
$font_size = 12;
// Set the offset x and y for the text position
$offset_x = 0;
$offset_y = 20;
// Get the size of the text area
$dims = imagettfbbox($font_size, 0, $font, $text);
$text_width = $dims[4] - $dims[6] + $offset_x;
$text_height = $dims[3] - $dims[5] + $offset_y;
// Add text background
imagefilledrectangle($image_p, 0, 0, $text_width, $text_height, $bg_color);
// Add text
imagettftext($image_p, $font_size, 0, $offset_x, $offset_y, $text_color, $font, $text);
// Save the picture
imagejpeg($image_p, $public_file_path . '/output.jpg', 100);
// Clear
imagedestroy($image);
imagedestroy($image_p);
};
Here is output
Try this. It will help you…
<?php
$img = imagecreatefromjpeg("girl-hugging-the-globe.jpg"); // image.jpg is the image on which we are going to write text ,you can replace this iamge name with your
if(!$img) die("Unabe to load image");
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$width = 600;// it will store width of image
$height = 100; //it will store height of image
$fontsize = 6; // size of font
$text = "Block Prints Online"; // Define the text
$pos = ( $width - imagefontwidth($fontsize)*STRLEN($text) );// calculate the left position of the text:
imagestring($img, $fontsize, 200, 150, $text, $red);
header('Content-type: image/jpeg');
imagejpeg($img);//it will output a jpeg image
imagedestroy($img);//it will destroy $img*/
?>