I need help (My english is not good, please try to understand what i'm trying to find). Currently i'm working in a project based on image transformation, where i'm using imagick to transform image. I'm successfully transformed my image. What i am doing in my project is taking a photo(photo1), placing it to another photo (photo2 {the grey are is transparent}) which create the following effect photo3. Then replaceing it again with photo2 (to create the mask effect which showing the hand), and the result is (photo4). But the problem is, photo1 area in photo4 gets darker. {Please compare photo3 and photo4}. Can anyone help me how to keep the original color of photo1 in photo4. Here is my code:
$image1 = new imagick( public_path("img/testImage2.jpg") );
$image2 = new imagick( public_path("storage/Templates/romantisch copy.png"));
$image3 = new imagick( public_path("storage/Templates/romantisch copy.png") );
//for preserving transparency
$image2->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);
$image3->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);
//resizing photo1
$image1->resizeImage(468, 729, Imagick::FILTER_LANCZOS, 1);
/* Fill background area with transparent */
$image1->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
// $image2->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_TRANSPARENT);
$image1->setImageArtifact('compose:args', "1,0,-0.5,0.5");
$image1->setImageMatte(true);
$controlPoints = array(
0, 0, //initial point (top left)
0, 0, //targeted point (top left)
0, $image1->getImageHeight(), //initial point (bottom left)
0, $image1->getImageHeight(), //targeted point (bottom left)
$image1->getImageWidth(), 0, //initial point (top right)
$image1->getImageWidth(), 0, //targeted point (top right)
$image1->getImageWidth(), $image1->getImageHeight(), //initial point (bottom right)
$image1->getImageWidth(), $image1->getImageHeight() //targeted point (bottom right)
);
// /* Perform the distortion */
$image1->distortImage(Imagick::DISTORTION_PERSPECTIVE, $controlPoints, true);
$image2->compositeImage($image1, Imagick::COMPOSITE_OVER, 720, 368);
$image2->compositeImage($image3, Imagick::COMPOSITE_OVER, 0, 0);
/* Ouput the image */
header("Content-Type: image/png");
$image2->writeImage ("test_0.png");
echo $image2;
Found my solution. Changing my colorspace solved the problem. still don't know why.
$image2->transformImageColorspace(\Imagick::COLORSPACE_XYZ);
$image3->transformImageColorspace(\Imagick::COLORSPACE_XYZ);
Related
I have the following scenario. There are two .png images with the exact same size. One is the background image and the other one is the overlay image.
The overlay consists of a transparent background and white letters.
I am trying to combine those two images into one so that I will end up having white text on the background image.
I tried it with this code:
$image_1 = imagecreatefrompng('newimages/'.$overla);
$imgFinal = imagecreatefrompng('imagebackrgounds/background.png');
imagealphablending($imgFinal, true);
imagesavealpha($imgFinal, false);
imagecopy($imgFinal, $image_1, 0, 0, 0, 0, 600, 579);
imagepng($imgFinal, 'new/new.png');
The code above only saves a white picture. What am I doing wrong here? Does someone have a working code?
This is the background picture:
This is the image with transparent background ( it should be on top of the background )
Solution:
imagecolortransparent($image_1,imagecolorat($image_1,0,0));
Full Code:
$image_1 = imagecreatefrompng('newimages/'.$overla);
imagecolortransparent($image_1,imagecolorat($image_1,0,0));
$imgFinal = imagecreatefrompng('imagebackrgounds/background.png');
imagealphablending($imgFinal, true);
imagesavealpha($imgFinal, false);
imagecopy($imgFinal, $image_1, 0, 0, 0, 0, 600, 579);
imagepng($imgFinal, 'new/new.png');
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 have two images. one is a jpg image of a rotated polaroid frame polaroid.jpg. The other is just an ordinary image image.jpg.
I'm trying to rotate the image, and then put it on top of the polaroid-image, and then show the merged images as one jpg-image.
I think I'm pretty close with the following code, but I can't manage to get the transparancy working. The uncovered zone of the rotated image is black instead of transparent. What am I doing wrong? I've added a comment to the lines that are relevant for getting a transparent background for the top-image.
$bg_src = "polaroid.jpg";
$img_src = "image.jpg";
$outputImage = imagecreatefromjpeg($bg_src);
$img = imagecreatefromjpeg($img_src);
// This should create transparent background.
$bgd_color = imagecolorallocatealpha($img, 0, 0, 0, 127);
// This should assign the transparent background to the uncovered zone after rotation
$img = imagerotate($img, 10, $bgd_color);
// This should make sure the alpha transparency gets saved
imagesavealpha($img, true);
$img_x = imagesx($img);
$img_y = imagesy($img);
imagecopymerge($outputImage,$img,156,50,0,0,$img_x,$img_y,100);
header('Content-type: image/jpeg');
imagejpeg($outputImage);
imagedestroy($outputImage);
Figured it out after some heave searching. Turns out to be real simple.
I just changed this line:
imagesavealpha($img, true);
to this:
imagecolortransparent($img,$bgd_color);
yay! :)
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);
}
?>