How to avoid black background when rotating an image 45 degrees using PHP?

Hi, I need to flip the thumpnail image before combining it with another jpeg file. but when i rotate 45 degrees using php. It shows a black background. how can i avoid this. any body can help me.

+4
source share
2 answers
<? $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 an alpha channel or are they completely opaque?

+1
source

Well, if you create jpg using PHP GD, you set the background color as the third version of the imagerotate function. In this example, I assume that you rotate the jpg image of $filename using arbitrary $angle degrees, and you need a white background, that is, color code 16777215 :

 $rotatedImage = imagerotate(imagecreatefromjpeg($filename), ((360-$angle)%360), 16777215); 

black is the color code 0 , which is the default, and the rest of the color scheme is between them, so you just need to decide what background color you need

EDIT: for a transparent background, if you create png you would do:

 $destimg = imagecreatefromjpeg($filename); $transColor = imagecolorallocatealpha($destimg, 255, 255, 255, 127); $rotatedImage = imagerotate($destimg, ((360-$angle)%360), $transColor); 

Hope that helps

+3
source

Source: https://habr.com/ru/post/1312404/


All Articles