sconsult / croppic

croppic
915 stars 229 forks source link

Background for img png is black color! #246

Open ttthiep2007 opened 7 years ago

ttthiep2007 commented 7 years ago

I used croppic, it is very necessary. But it has not been stable 1 seats. That is the background cropped png images of it was black. I've got to fix this bug then. Would that I could send my code that has a fix for you is it?

dinesh-kongo commented 7 years ago

Hi, Can you please share that fix?

ttthiep2007 commented 7 years ago

1. At line 61 of file img_crop_to_file.php I added a few lines of code as follows: Before:

// resize the original image to size of editor $resizedImage = imagecreatetruecolor($imgW, $imgH); imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);

After:

// resize the original image to size of editor $resizedImage = imagecreatetruecolor($imgW, $imgH); if ($type == '.png') { @imagealphablending($resizedImage, false); @imagesavealpha($resizedImage, true); } else { $white = @imagecolorallocate($resizedImage, 255, 255, 255); @imagefill($resizedImage, 1, 1, $white); } imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);

2. At line 62 of file img_crop_to_file.php I added a few lines of code as follows: Before:

// crop rotated image to fit into original rezized rectangle $cropped_rotated_image = imagecreatetruecolor($imgW, $imgH); imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0)); imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);

After:

// crop rotated image to fit into original rezized rectangle $cropped_rotated_image = imagecreatetruecolor($imgW, $imgH); if ($type == '.png') { @imagealphablending($cropped_rotated_image, false); @imagesavealpha($cropped_rotated_image, true); } else { $white = @imagecolorallocate($cropped_rotated_image, 255, 255, 255); @imagefill($cropped_rotated_image, 1, 1, $white); } imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);

3. At line 76 of file img_crop_to_file.php I added a few lines of code as follows: Before:

// crop image into selected area $final_image = imagecreatetruecolor($cropW, $cropH); imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0)); imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);

After:

$final_image = imagecreatetruecolor($cropW, $cropH); if ($type == '.png') { @imagealphablending($final_image, false); @imagesavealpha($final_image, true); } else { $white = @imagecolorallocate($final_image, 255, 255, 255); @imagefill($final_image, 1, 1, $white); } imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);

4. At line 80 of file img_crop_to_file.php I added a few lines of code as follows: Before:

// finally output png image //imagepng($final_image, $output_filename.$type, $png_quality); imagejpeg($final_image, $output_filename.$type, $jpeg_quality); $response = Array( "status" => 'success', "url" => $output_filename.$type );

After:

// finally output png image
switch ($type) { case '.jpg': imagejpeg($final_image, $output_filename.$type, $jpeg_quality); break; case '.png': imagepng($final_image, $output_filename.$type); break; case '.gif': @imagegif($final_image, $output_filename.$type, 100); break; } $response = Array( "status" => 'success', "url" => $output_filename.$type );

KongoLakshit commented 7 years ago

Hi @ttthiep2007

Thanks for the response, it is quit useful.

Thanks again!

andresteves commented 6 years ago

Hi @ttthiep2007 I have changed but the background keep being black. Are you using any specific library?