I want to paste the $original image into the center of $fondo image, i write the following code:
<?php header('Content-Type: image/png'); // The file $fondo = imagecreate(1000,1000); // Create 1000x1000 image $color_fondo = imagecolorallocate($fondo,197,237,206); // Set color of background $original = imagecreatefromstring(file_get_contents('test.jpg')); // Load image $wb = imagesx($fondo); // Bakground width $wi = imagesx($original); // Image width $hb = imagesy($fondo); $hi = imagesy($original); //Want to center in the middle of the image, so calc ($wb/2-$wi/2) imagecopy($fondo,$original,($wb/2-$wi/2),($hb/2-$hi/2),0,0,imagesx($original),imagesy($original)); imagepng($fondo);
The result i got is this:
As you can see the cyan color is affecting original image:
Any ideas on what i’m wrong? Thank you!
Advertisement
Answer
the solution is:
Creating the image as imagecreatetruecolor instead of imagecreate
$fondo = imagecreatetruecolor(1000,1000); // Create 1000x1000 image $color_fondo = imagecolorallocate($fondo,197,237,206); //Desired color
Then, need to paint the $fondo with imagefill($fondo,0,0,$color_fondo);
Also, crop as circle using this function
function circulo ($original,$radio){ $src = imagecreatefromstring(file_get_contents($original)); $w = imagesx($src); $h = imagesy($src); $newpic = imagecreatetruecolor($w,$h); imagealphablending($newpic,false); $transparent = imagecolorallocatealpha($newpic, 0, 0, 0, 127); //Hacer transparente la imagen $r=$w/$radio; //Radio del circulo for($x=0;$x<$w;$x++) for($y=0;$y<$h;$y++){ $c = imagecolorat($src,$x,$y); $_x = $x - $w/2; // echo $_x."n"; $_y = $y - $h/2; if((($_x*$_x) + ($_y*$_y)) < ($r*$r)){ imagesetpixel($newpic,$x,$y,$c); }else{ imagesetpixel($newpic,$x,$y,$transparent); } } return $newpic; }
The result is :