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
Related
I want to flood fill the frame with tiled image instead of color. Currently im using this codes
$imagick = new \Imagick('image.jpg');
$imagick->scaleImage(300, 300, false);
// Create frame placeholder
$imagick->frameimage( 'red','30','30', 30, 0);
// Flood fill with color
$imagick->floodFillPaintImage('green', 10, '#6e0000',0, 0,false
);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
and the result:
but i want a result like this
Thank you very much!
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
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.
I am trying to merge two images in PHP, overlapping each other in the middle like in here : http://i.stack.imgur.com/ejdDQ.jpg
However, I am having no luck.
On the ImageMagick page, they are using this method to do it in the command line:
http://www.imagemagick.org/Usage/photos/#overlap
But since I cannot use exec on my server, I have to do it somehow with the help of the imagick extension installed on the server (http://us1.php.net/manual/en/book.imagick.php).
Is there any way to make this work?
Using the source files from the link and the code below generates the image:
//Load the images
$left = new Imagick(realpath('../images/im/holocaust_tn.gif'));
$right = new Imagick(realpath('../images/im/spiral_stairs_tn.gif'));
$gradient = new Imagick(realpath('../images/im/overlap_mask.png'));
//The right bit will be offset by a certain amount - avoid recalculating.
$offsetX = $gradient->getImageWidth() - $right->getImageWidth();
//When doing the fading of the images, ImageMagick uses "White == show image".
//The gradient is setup with black on the left, white on the right. So the for
//the left image we need to reverse the gradient to make it white on the left.
$negativeGradient = clone $gradient;
$negativeGradient->negateimage(false);
//Fade out the left part
$left->compositeimage(
$negativeGradient,
Imagick::COMPOSITE_COPYOPACITY,
0, 0
);
//Fade out the right part - offset the gradient
//to make it align in the final image
$right->compositeimage(
$gradient,
Imagick::COMPOSITE_COPYOPACITY,
-$offsetX, 0
);
//Create a new canvas to render everything in to.
$canvas = new Imagick();
$canvas->newImage($gradient->getImageWidth(), $gradient->getImageHeight(), new ImagickPixel('black'));
//Blend left half into final image
$canvas->compositeimage(
$left,
Imagick::COMPOSITE_BLEND,
0, 0
);
//Blend Right half into final image
$canvas->compositeimage(
$right,
Imagick::COMPOSITE_BLEND,
$offsetX, 0
);
//Output the final image
$canvas->setImageFormat('png');
header("Content-Type: image/png");
echo $canvas->getImageBlob();
// And Robert is your father's brother.
I'm trying to spice up my website by using custom fonts for headings. For me, the most appropriate way to do this is using PHP and GD. I have written a little script which will output the dynamic text based on the $_GET value, however sometimes the image is too wide, which moves everything else about.
How can I get the image to adjust the width of it, based on the width of the text? Here is the code I've written so far:
<?php
// Settings
$sText = $_GET['t']; // Text of heading
$sFont = "font/AvantGarde-Book.ttf"; // Default font for headings
$sMain = $_GET['c'] ? $_GET['c'] : 0xF2AB27; // Get a font or default it
// Create the image
header("content-type: image/png"); // Set the content-type
$hImage = imagecreatetruecolor(200, 24);
ImageFill($hImage, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($hImage, true);
imagealphablending($hImage, false);
imagettftext($hImage, 20, 0, 0, 24, $sMain, $sFont, $sText); // Draw the text
imagepng($hImage); // Generate the image
imagedestroy($hImage); // Destroy it from the cache ?>
Thanks!
Ok, I figured it out! For anyone else who may have this problem, you need to add:
// Calcuate the width of the image
$arSize = imagettfbbox(24, 0, $sFont, $sText);
$iWidth = abs($arSize[2] - $arSize[0]);
$iHeight = abs($arSize[7] - $arSize[1]);
Before the imagecreatetruecolor()
The function imagettfbbox will calculate the size of what the text will be based on the font you picked. Use the results in your call to imagecreatetruecolor.