krishna
krishna

Reputation: 923

Cropping an image in PHP

I am doing PNG image cropping using this function. Everything is working fine, but whenever I upload the .png, the image background color is changed to black. Here is the code.

$bgimage_attribs = getimagesize($bgim_file_name);

        if($filetype3=='image/gif') 
            {
                $bgim_old = imagecreatefromgif($bgim_file_name); 
            }   
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            {
                 $bgim_old = imagecreatefromjpeg($bgim_file_name);
            }
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            {
                 $bgim_old = imagecreatefrompng($bgim_file_name);
            }

        $bgth_max_width =265; //for Album image
        $bgth_max_height =150;
        $bgratio = ($bgwidth > $bgheight) ? $bgth_max_width/$bgimage_attribs[0] : $bgth_max_height/$bgimage_attribs[1];

        $bgth_width =265;//$image_attribs[0] * $ratio; 
        $bgth_height =150;//$image_attribs[1] * $ratio;

        $bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); 
        imageantialias($bgim_new,true); 
        $bgth_file_name = "partners_logs/250x150/$Attachments3";
        imagecopyresampled($bgim_new,$bgim_old,0,0,0,0,$bgth_width,$bgth_height, $bgimage_attribs[0], $bgimage_attribs[1]);
        if($filetype3=='image/gif') 
            {
                imagegif($bgim_new,$bgth_file_name,100);
                //$bgim_old = imagegif($bgim_file_name); 
            }   
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            {
                 imagejpeg($bgim_new,$bgth_file_name,100);
            }
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            {
                 imagepng($bgim_new,$bgth_file_name,9);
            }           `

even i tried like this also

/************************************Resizing the image 80*60****************/
        $path3="partners_logs";
        $filetype3=$_FILES["pimage"]["type"];
        $bgim_file_name = $path3."/".$Attachments2; 
        $bgimage_attribs = getimagesize($bgim_file_name);

        if($filetype3=='image/gif') 
            {
                $bgim_old = imagecreatefromgif($bgim_file_name); 
            }   
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            {
                 $bgim_old = imagecreatefromjpeg($bgim_file_name);
            }
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            {
                 $bgim_old = imagecreatefrompng($bgim_file_name);
                 imageAlphaBlending($bgim_old, true);

imageSaveAlpha($bgim_old, true); }

        $bgth_max_width =265; //for Album image
        $bgth_max_height =150;
        $bgratio = ($bgwidth > $bgheight) ? $bgth_max_width/$bgimage_attribs[0] : $bgth_max_height/$bgimage_attribs[1];

        $bgth_width =265;//$image_attribs[0] * $ratio; 
        $bgth_height =150;//$image_attribs[1] * $ratio;

        $bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); 
        imageantialias($bgim_new,true); 
        $bgth_file_name = "partners_logs/250x150/$Attachments2";
        imagecopyresampled($bgim_new,$bgim_old,0,0,0,0,$bgth_width,$bgth_height, $bgimage_attribs[0], $bgimage_attribs[1]);
        if($filetype3=='image/gif') 
            {
                imagegif($bgim_new,$bgth_file_name,100);
                //$bgim_old = imagegif($bgim_file_name); 
            }   
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            {
                 imagejpeg($bgim_new,$bgth_file_name,100);
            }
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            {
                 imagepng($bgim_new,$bgth_file_name,9);
                   //set the background color to your choice, paramters are int values of red,green and blue  

imagecolorallocate($bgim_new,0xFF,0xFF,0xFF); }
/************End Resize of 80*60*******************/

Upvotes: 0

Views: 2025

Answers (2)

Ahmad Sattar
Ahmad Sattar

Reputation: 321

Try This Function....

<?php
function CropImg($img_dst, $img_src, $dst_width = 300, $dst_height = 180){
$ext = pathinfo($img_src, PATHINFO_EXTENSION);
if($ext != 'jpg' and $ext != 'jpeg'){
    $image = imagecreatefrompng($img_src);

}else{
    $image = imagecreatefromjpeg($img_src);
}
$filename = $img_dst;

$thumb_width = $dst_width;
$thumb_height = $dst_height;

$width = imagesx($image);
$height = imagesy($image);

$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;

if ( $original_aspect >= $thumb_aspect )
{
   // If image is wider than thumbnail (in aspect ratio sense)
   $new_height = $thumb_height;
   $new_width = $width / ($height / $thumb_height);
}
else
{
   // If the thumbnail is wider than the image
   $new_width = $thumb_width;
   $new_height = $height / ($width / $thumb_width);
}

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

if($ext == 'png'){
    imagealphablending($thumb, false);
    $white = imagecolorallocatealpha($thumb, 0, 0, 0, 127);    //FOR WHITE BACKGROUND
    imagefilledrectangle($thumb,0,0,$thumb_width,$thumb_height,$white);
    imagesavealpha($thumb, true);
}

// Resize and crop
imagecopyresampled($thumb,
                   $image,
                   0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
                   0 - ($new_height - $thumb_height) / 2, // Center the image vertically
                   0, 0,
                   $new_width, $new_height,
                   $width, $height);
if($ext == 'png'){
    imagepng($thumb, $filename);
}else{
    imagejpeg($thumb, $filename, 80);
}
return true;
}

CropImg("photo.png", "result.png", 300, 180);
?>

Upvotes: 1

teemitzitrone
teemitzitrone

Reputation: 2260

maybe this helps

Don't forget about imagealphablending() and imagesavealpha() if you're working with [semi]transparent png.

<?php
$file = 'semitransparent.png'; // path to png image
$img = imagecreatefrompng($file); // open image
imagealphablending($img, true); // setting alpha blending on
imagesavealpha($img, true); // save alphablending setting (important)

found at php.net - imagecreatefrompng under User Contributed Notes

NOT tested

edit see comment

<?php
$bgim_new = imagecreatetruecolor($bgth_width,$bgth_height);
imageantialias($bgim_new,true);
imageAlphaBlending($bgim_new, true);
imageSaveAlpha($bgim_new, true);
?>

After some testing and reading througt several pnp.net comments i've got a working script

<?php
$newImageName = "PNG_transparency_copy.png";

$newWidth = 265;
$newHeight = 150;

$orgAttribs = getimagesize("PNG_transparency_demonstration_1.png");

$orginal = imagecreatefrompng("PNG_transparency_demonstration_1.png");
imagesavealpha($orginal, true);

$newPng = imagecreatetruecolor($newWidth,$newHeight);

imagealphablending($newPng, false);
$color = imagecolortransparent($newPng, imagecolorallocatealpha($newPng, 0, 0, 0, 127));
imagefill($newPng, 0, 0, $color);
imagesavealpha($newPng, true);

imagecopyresampled($newPng,$orginal,0,0,0,0,$newWidth,$newHeight, $orgAttribs[0], $orgAttribs[1]);

header("Content-type: image/png");
imagepng($newPng,$newImageName);

PNG taken from Wikipedia

Upvotes: 3

Related Questions