I'm trying to merge of this images:
https://imgur.com/aURQax9 -- Base
https://imgur.com/a/cpiSc -- Marking
the result should look like this:
https://imgur.com/a/ri3zw
I'm getting an image that is removing all of the black color, but I'm unsure how to do this.
$numberOfImages = 2;
$x = 600;
$y = 600;
$background = imagecreatetruecolor($x, $y);
$black = imagecolorallocate($background, 0, 0, 0);
imagecolortransparent($background, $black);
$firstUrl = 'Images/Horses/First horses/Red Breeds/Paint/Adult/Overo/1/BayOvero1AD.png';
$secondUrl = 'Images/Horses/First horses/Red Breeds/Paint/Markings/PaintBlazeAD.png';
$outputImage = $background;
$first = imagecreatefrompng($firstUrl);
$second = imagecreatefrompng($secondUrl);
imagecopymerge($outputImage,$first,0,0,0,0, $x, $y,100);
imagecopymerge($outputImage,$second,0,$y,0,0, $x, $y,100);
imagepng($outputImage, './Images/BayOvero1AD.PaintBlazeAD.png');
imagedestroy($outputImage);
How can I update this so that the color isn't removed and that it merges very similarly to the finished image above?
I have just wrote this for you. One of the images posted was a jpg not a PNG, however that could be to do with imgur, therefore I had to resize it and remove some of the white. Which is why my result didn't look quite right when testing. But it should be fine with your original files:
As you can see, it added the second image above the first, and kept it in the correct posistion.
The code I used for this was:
<?php
$x = 600;
$y = 600;
$firstUrl = 'Images/Horses/First horses/Red Breeds/Paint/Adult/Overo/1/BayOvero1AD.png';
$secondUrl = 'Images/Horses/First horses/Red Breeds/Paint/Markings/PaintBlazeAD.png';
$Image1 = imagecreatefrompng($firstUrl);
$Image2 = imagecreatefrompng($secondUrl);
imagealphablending($Image1, true);
imagesavealpha($Image1, true);
imagecopy($Image1, $Image2, 0, 0, 0, 0, $x, $y);
imagepng($Image1, './Images/BayOvero1AD.PaintBlazeAD.png');
?>
Related
EDIT: please forget about this question! I made a stupid error in the original code.
The example code works as expected!
I'm trying to rotate AND crop images.
I have this so far:
$w = 100;
$h = 400;
$img1 = imagecreatefromjpeg('image1.jpg');
$img2 = imagecreatefromjpeg('image2.jpg');
for ($i = 0; $i < 2; $i++) {
${'img'.$i + 3} = imagecreatetruecolor($h, $h);
imagecopy(${'img'.$i + 3}, ${'img'.$i + 1}, 0, 0, 0, 0, $w, $h);
${'img'.$i + 3} = imagerotate(${'img'.$i + 3}, 90, 0);
${'img'.$i + 3} = imagecrop(${'img'.$i + 3}, array(0, 0, $h, $w));
imagejpeg(${'img'.$i + 3});
imagedestroy(${'img'.$i + 3});
imagedestroy(${'img'.$i + 1});
}
So what I essentially do is open some JPGs, create new images, copy the JPGs into the new images and then crop the images.
Alas this results in empty images ...
What am I doing wrong?
No idea if this will make any difference to the lack of output - but what do $img1 & $img2 do - they don't get used as far as i can see?
#error_reporting( E_ALL );
$w = 100;
$h = 400;
$img1 = imagecreatefromjpeg('image1.jpg');
$img2 = imagecreatefromjpeg('image2.jpg');
for ($i = 0; $i < 3; $i++) {
$new=${'img'.$i + 3};
$src=${'img'.$i + 1};
$new = imagecreatetruecolor($h, $h);
imagecopy( $new, $src, 0, 0, 0, 0, $w, $h);
$new = imagerotate($new, 90, 0);
$new = imagecrop($new, array(0, 0, $h, $w));
imagejpeg($new);
imagedestroy($new);
imagedestroy($src);
break;/* just to see if it gets this far*/
}
Firstly, you should consider using image copy resampled for a better quality of result from the process.
Then, you need to correctly use the imagejpeg function, currently you are simply loading the JPGs and outputting them directly, which may be what you want but you're in a loop and you can't loop load multiple images directly to the same file. It also means that the image you're seeing (or not) is the final image in the set.
Your Problem is that your for loop runs THREE times but your only have data associated with the first two instances, but the third instance is empty, and as this is the most recent this is the only instance output to the browser.
So:
1) Save the images you have generated with imagejpeg($data,$filename);. You can define a filename as $filename = $img+3.".jpg"; or similar.
2) It would also be much easier to debug and read your code if you used arrays instead of numerically incremented variables, that's a very messy way of writing code!
3) If you do want to output the image directly to the browser you need PHP to supply a header such as header('Content-Type: image/jpeg'); before outputting the contents of imagejpeg.
A Rehash of your code:
$w = 100;
$h = 400;
$img[1] = imagecreatefromjpeg('image1.jpg');
$img[2] = imagecreatefromjpeg('image2.jpg');
for ($i = 3; $i < 5; $i++) {
$img[$i] = imagecreatetruecolor($h, $h);
$j = $i -2;
imagecopyresampled($img[$i], $img[$j], 0, 0, 0, 0, $h, $h, $w, $h,);
// this function also contains destination width and destination
// height, which are equal to the size of the destination image so
// are set as $h, $h in the function above.
$img[$i] = imagerotate($img[$i], 90, 0);
$img[$i] = imagecrop($img[$i], array(0, 0, $h, $w));
$filename = "image-".$i.".jpg";
imagejpeg($img[$i], $filename);
imagedestroy($img[$i]);
imagedestroy($img[$j]);
}
I want to add a text to a image. The text should be displayed in multiple areas of the image (not just one).
For example I want to watermark with a text stack. Stack should be displayed in the image at least 8 times in different areas in the image.
I just learned about imagestring() and imagettftext(), but these two only displays my text on a single spot.
Image is not fixed size, so i cannot specify exact and multiple location in advance. It should work on all sizes of images
<?php
/*
image.php
*/
header("Content-type: image/jpeg");
$imgPath = 'olximage.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = "stack overflow";
$fontSize = 3;
$x = 15;
$y = 185;
imagestring($image, $fontSize, $x, $y, $string, $color);
$x = 15;
$y = 175;
imagestring($image, $fontSize, $x, $y, $string, $color);
imagejpeg($image);
?>
Thanks in advance
For example:
<?php
/*
image.php
*/
header("Content-type: image/jpeg");
$imgPath = 'olximage.jpg';
$image = imagecreatefromjpeg($imgPath);
$color = imagecolorallocate($image, 255, 255, 255);
$string = "stack overflow";
$fontSize = 3;
$imageHeight = imagesy($image);
$distanceY = 10;
$maxImageStrings = max(8, $imageHeight / $distanceY);
$x = 15;
for ($i = 0; $i < $maxImageStrings; $i++) {
$y = $i * $distanceY;
imagestring($image, $fontSize, $x, $y, $string, $color);
}
imagejpeg($image);
You can finetune calculations for your needs.
I'm using Imagick extension for same. If you want to go with this then follow detail:
PHP:
// Create objects
$image = new Imagick('image.png');
$watermark = new Imagick();
// Watermark text
$text = 'Copyright';
// Create a new drawing palette
$draw = new ImagickDraw();
$watermark->newImage(140, 80, new ImagickPixel('none'));
// Set font properties
$draw->setFont('Arial');
$draw->setFillColor('grey');
$draw->setFillOpacity(.5);
// Position text at the top left of the watermark
$draw->setGravity(Imagick::GRAVITY_NORTHWEST);
// Draw text on the watermark
$watermark->annotateImage($draw, 10, 10, 0, $text);
// Position text at the bottom right of the watermark
$draw->setGravity(Imagick::GRAVITY_SOUTHEAST);
// Draw text on the watermark
$watermark->annotateImage($draw, 5, 15, 0, $text);
// Repeatedly overlay watermark on image
for ($w = 0; $w < $image->getImageWidth(); $w += 140) {
for ($h = 0; $h < $image->getImageHeight(); $h += 80) {
$image->compositeImage($watermark, Imagick::COMPOSITE_OVER, $w, $h);
}
}
// Set output image format
$image->setImageFormat('png');
// Output the new image
header('Content-type: image/png');
echo $image;
although there are plenty of command-line examples to be found on the ImageMagick website, so that is where we shall begin.
I want to fill the png non transparent part with any color or image using php.
Following is the base image
Following is the target image
I have used following code of php to fill non transparent part of the png.
$im = imagecreatefrompng(dirname(__FILE__) . '/images/cat_1.png');
$red = imagecolorallocate($im, 255, 0, 0);
imagefill($im, 0, 0, $red);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
But it gives me following output.
please help me to complete my task.
Thanks in advance.
Save this version of the base image:
This image has been saved as an indexed-PNG format, making it perfect for colour substitution. In this case, index 0 is the cat's colour, and index 1 is the background (not ideal, but that's what GIMP gave me)
In this case:
$img = imagecreatefrompng("cat_1.png");
imagecolorset($img,0, 255,0,0);
imagepng($img); // output red cat
Image editing in general is made much easier if you have a base image that lends itself to easy editing in this way ;)
This line imagefill($im, 0, 0, $red); fills the image with the color red, at the coordinate (0,0), which is topleft. So it starts filling at the top left and fills everything like when you use the paintbucket in MSPaint. You could use imagefill($im, 150, 150, $red); for example (if 150,150 is the center).
use this function, it returns a base64 image <img src="output">
public static function colorImage($url, $hex = null, $r = null, $g = null, $b = null)
{
if ($hex != null) {
$hex = str_replace("#", "", $hex);
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
}
$im = imagecreatefrompng($url);
imageAlphaBlending($im, true);
imageSaveAlpha($im, true);
if (imageistruecolor($im)) {
$sx = imagesx($im);
$sy = imagesy($im);
for ($x = 0; $x < $sx; $x++) {
for ($y = 0; $y < $sy; $y++) {
$c = imagecolorat($im, $x, $y);
$a = $c & 0xFF000000;
$newColor = $a | $r << 16 | $g << 8 | $b;
imagesetpixel($im, $x, $y, $newColor);
}
}
}
ob_start();
imagepng($im);
imagedestroy($im);
$image_data = ob_get_contents();
ob_end_clean();
$image_data_base64 = "data:image/png;base64," . base64_encode($image_data);
return $image_data_base64;
}
I am trying to change a section of text into an image. I can't figure out what is wrong with my code and also how to make it transparent. Here is what I have so far:
<?PHP
function fsrep_imageaddress($address, $listingid) {
$font_size = 4;
$width = imagefontwidth($font_size)*strlen($address);
$height = imagefontheight($font_size);
$img = imagecreate($width,$height);
$bg = imagecolorallocate($img, 25, 25, 25);
$color = imagecolorallocate($img, 255, 255, 255);
$len = strlen($address);
$ypos = 0;
for($i=0;$i<$len;$i++){
$xpos = $i * imagefontwidth($font_size);
imagechar($img, $font_size, $xpos, $ypos, $address, $color);
$address = substr($address, 1);
}
imagepng($img, ABSPATH.'wp-content/uploads/fsrep/houses/address-'.$listingid.'.png',100);
imagedestroy($img);
}
fsrep_imageaddress(Just Testing, 12)
?>
Why is it not working?
If you look in the error log, you'll see you need to put "Just Testing" in quotes (thanks #scrowler!) in the function call. Then you'll get an error that imagepng takes a quality level of 0 (no compression) to 9 (max). You have 100! Here, I set it to 5 (medium compression).
imagepng($img, ABSPATH.'wp-content/uploads/fsrep/houses/address-'.$listingid.'.png',5);
How do I set a transparent background?
There's a weird thing with palette images (ie those created with imagecreate). The first colour allocated is set as the background, and can't be transparent - so you need to create a dummy color and then convert it to transparent.
// after this line
$bg = imagecolorallocate($img, 25, 25, 25);
// add this
imagecolortransparent($img, $bg);
Result
I made these changes and changed the text to red (255,0,0) for readability and got this:
I'm trying to use the following PHP in order to apply randomly generated text files to an image. (I'm just using a random image right now.)
<?php
header ("Content-type: image/png");
$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}
//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
$x = imagesx($im) - $width ;
$y = imagesy($im) - $height;
$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
$textColor = imagecolorallocate ($im, 0, 0,0);
imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
ImageDestroy($im);
?>
However, when I run that code the imported image just becomes very blockish.
Here is the image I am testing with:
http://i.stack.imgur.com/LhNkv.png
And here is how it actually appears:
http://i.stack.imgur.com/AAcHZ.png
My research shows that "imagecreate" generates a "palette" image - and I thought that might have something to do with my error, but I have seen plenty of examples where the base image is by no means distorted.
Thanks in advance for your insights.
(Ugh. It wouldn't let me post images but let me upload them just fine?)
Update
Changing the code to:
<?php
header ("Content-type: image/png");
$textfile = "quote.txt";
$quotes = array();
if(file_exists($textfile)){
$quotes = file($textfile);
srand ((float) microtime() * 10000000);
$string = $quotes[array_rand($quotes)];
$string = substr($string,0,strlen($string)-1);
}
else{
$string = "No 'Quote' available at this time.";
}
//$string = "your text";
// try changing this as well
$font = 4;
$width = imagefontwidth($font) * strlen($string) ;
$height = imagefontheight($font) ;
$im = imagecreatefrompng("test.png");
//$x = imagesx($im) - $width ;
//$y = imagesy($im) - $height;
//$backgroundColor = imagecolorallocate ($im, 255, 255, 255);
//$textColor = imagecolorallocate ($im, 0, 0,0);
//imagestring ($im, $font, $x, $y, $string, $textColor);
imagepng($im);
ImageDestroy($im);
?>
Produces the same block-like effects as above, except now no text is being written to the image, either (obviously?).
Could be an alpha blending problem. Try adding these before saving the image:
imagealphablending($im, true);
imagesavealpha($im, true);