Return to Snippet

Revision: 60381
at November 6, 2012 17:30 by thescorpion


Initial Code
$myfile=$_FILES["youimage"];
function ismyimage($myfile)
{
    if((($myfile["type"] == "image/gif") || ($myfile["type"] == "image/jpg") || ($myfile["type"] == "image/jpeg") || ($myfile["type"] == "image/png")) && ($myfile["size"] <= 2097152 /*2mb*/) ) return true; 
    else return false;
}

function upload_file($myfile)
{           

            if(ismyimage($myfile)){
                $information=getimagesize($myfile["tmp_name"]);
                $mywidth=$information[0];
                $myheight=$information[1];

                $newwidth=$mywidth;
                $newheight=$myheight;
                while(($newwidth > 600) || ($newheight > 400 )){
                        $newwidth = $newwidth-ceil($newwidth/100);
                        $newheight = $newheight-ceil($newheight/100);
                    } 

                $files=$myfile["name"];

                if($myfile["type"] == "image/gif"){
                $tmp=imagecreatetruecolor($newwidth,$newheight);
                $src=imagecreatefromgif($myfile["tmp_name"]);
                imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $mywidth, $myheight);
                $con=imagegif($tmp, $files);
                imagedestroy($tmp);
                imagedestroy($src);
                if($con){return true;} else {return false;}
                }
                else if(($myfile["type"] == "image/jpg") || ($myfile["type"] == "image/jpeg") ) {
                $tmp=imagecreatetruecolor($newwidth,$newheight);
                $src=imagecreatefromjpeg($myfile["tmp_name"]); 
                imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $mywidth, $myheight);
                $con=imagejpeg($tmp, $files);
                imagedestroy($tmp);
                imagedestroy($src);
                if($con){  return true;} else {return false;}
                    }
                else if($myfile["type"] == "image/png") {
                $tmp=imagecreatetruecolor($newwidth,$newheight);
                $src=imagecreatefrompng($myfile["tmp_name"]);
                imagealphablending($tmp, false);
                imagesavealpha($tmp,true);
                $transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
                imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent); 
                imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $mywidth, $myheight);
                $con=imagepng($tmp, $files);
                imagedestroy($tmp);
                imagedestroy($src);
                if($con){ return true;} else {return false;}
                    }

                }
                else return false;
}

Initial URL
http://stackoverflow.com/questions/32243/can-png-image-transparency-be-preserved-when-using-phps-gdlib-imagecopyresample

Initial Description
AL subir imagen png, se deben pasar al directorio con transparencia

Initial Title
PHP Upload imagen con thumbs resize and transparent png

Initial Tags
image

Initial Language
PHP