Hi I have to flip a thumpnail image before merge it with another jpeg file. but when I rotate 45 degree using php. It shows a black background. how can I avoid that. any body can help me.
views:
228answers:
2
+3
A:
Well, if you are generating a jpg, using PHP GD you set the color of the background as the third option of the function imagerotate
. In this example I'm gonna assume that you are rotating a jpg image $filename
by an arbitrary $angle
degrees, and you want a white background, i.e. color code 16777215
:
$rotatedImage = imagerotate(imagecreatefromjpeg($filename), ((360-$angle)%360), 16777215);
black is color code 0
, which is default, and the rest of the color gamma is in between the two, so you just need to decide which background color you would like
EDIT: for transparent backgrounds, if you are generating a png you would do:
$destimg = imagecreatefromjpeg($filename);
$transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127);
$rotatedImage = imagerotate($destimg, ((360-$angle)%360), $transColor);
Hope that helps
Raine
2010-06-10 06:31:10
How can I make it as transparent. If I gave -1 it will also display white back ground.
learner
2010-06-10 06:45:08
@learner: Like I stated, you can't. JPEG images do not support transparent backgrounds.
animuson
2010-06-10 06:47:54
can I change a jpeg file to gif format when it is uploaded. do you have any other suggestions for me.
learner
2010-06-10 06:53:16
@learner: animuson is correct in that you can't have transparent colors if the image you are generating is a jpeg, however if you would like to generate a png (heavier file size), then I would recommend checking out this page, where you can get ideas from the users' comments at the bottom of page http://us.php.net/manual/en/function.imagerotate.php
Raine
2010-06-10 06:55:14
@learner: yes, you may, see my comment and check out imagepng function at http://us.php.net/manual/en/function.imagepng.phpI would stay away from gif though, but png is the way to go
Raine
2010-06-10 07:05:41
Thanks for your valuable comments.
learner
2010-06-10 07:22:30
hello raine how can we make the background portion as transperant after rotating 25 degree. now i am using .png images but its not working with your codes. It still shows the black background. Please help me :(
learner
2010-06-14 09:25:17
@learner: could you post the updated code you are using? The code I posted works in my app, so I'd just like to take a look and see if i spot anything in yours that prevents it from setting a transparent background. Thx
Raine
2010-06-15 00:40:30
Hi Raine this is my code$image = "130.jpg";//Hi raine this is my code I select all image details from the db. that is $data array. $degrees = 25;for($i=0;$i<count($data);$i++){ $ext = ""; $extarr = ""; $extarr = explode(".", $data[$i]['name']); $ext = array_pop($extarr); if($ext == "png"){ $rotate = imagecreatefrompng("images/".$data[$i]['name']); // create watermark $transColor = imagecolorallocatealpha($rotate, 255, 255, 255, 270); $watermark1[$i] = imagerotate($rotate, ((360-$degrees)%360), $transColor); } }
learner
2010-06-17 06:02:52
##after rotation I have to merge it with one main image. for($i=0; $i<count($watermark1); $i++){ if($i == 0)imagecopymerge($image, $watermark1[$i], $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity); else imagecopymerge($image, $watermark1[$i], ($i*$dest_x)*3, ($i*$dest_y)*15, 0, 0, $watermark_width, $watermark_height, $opacity); imagedestroy($watermark1[$i]); }//finally display the imageheader("content-type: image/png"); imagepng($image); imagedestroy($image); //it display the image but that black portion still exist
learner
2010-06-17 06:03:08
I see what you are doing. Even though it seems it should work, you can't just do an imagecopymerge, as it doesn't handle transparency of the png stamp correctly. I did a project a bit ago where I had to use a little workaround specifically for this. I will look it up tomorrow morning when I get to work and post the code here.
Raine
2010-06-17 08:46:22
@learner: See my second answer above. The function imagecopymerge_alpha is the workaround to properly handle the alpha channel when superimposing PNGs, as imagecopymerge has always been bugged in that regard
Raine
2010-06-17 16:28:08
+1
A:
<?
$image = "130.jpg";
$degrees = 25;
for($i=0;$i<count($data);$i++){
$ext = "";
$extarr = "";
$extarr = explode(".", $data[$i]['name']);
$ext = array_pop($extarr);
if($ext == "png"){
$rotate = imagecreatefrompng("images/".$data[$i]['name']);
$transColor = imagecolorallocatealpha($rotate, 255, 255, 255, 270);
$watermark1[$i] = imagerotate($rotate, ((360-$degrees)%360), $transColor);
}
}
function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $opct){
$w = imagesx($src_im);
$h = imagesy($src_im);
$cut = imagecreatetruecolor($src_w, $src_h);
imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
imagecopymerge($dst_im, $cut, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, (100 - $opct));
}
for($i=0; $i<count($watermark1); $i++){
if($i == 0) imagecopymerge_alpha($image, $watermark1[$i], $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
else imagecopymerge_alpha($image, $watermark1[$i], ($i*$dest_x)*3, ($i*$dest_y)*15, 0, 0, $watermark_width, $watermark_height, $opacity);
imagedestroy($watermark1[$i]);
}
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
?>
Also, do your watermark images have alpha channel or are they fully opaque?
Raine
2010-06-17 16:20:00