禁用gif调整大小或调整大小而不丢失动画

时间:2014-06-25 13:33:13

标签: php upload gd gif animated-gif

我在互联网上找到了这个代码,一切正常,但我不想调整gif图像的大小,因为当我调整动画gif文件的大小时,会破坏动画。或者,如果很容易调整动画gif图像大小而不丢失动画,

这是upload.php文件

if(isset($_POST))
{
    ############ Edit settings ##############
    $BigImageMaxSize        = 500; //Image Maximum height or width
    $ThumbPrefix            = "thumb_"; //Normal thumb Prefix
    $DestinationDirectory   = 'uploads/'; //specify upload directory ends with / (slash)
    $Quality                = 90; //jpeg quality
    ##########################################

    //check if this is an ajax request
    if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])){
        die();
    }

    // check $_FILES['ImageFile'] not empty
    if(!isset($_FILES['ImageFile']) || !is_uploaded_file($_FILES['ImageFile']['tmp_name']))
    {
            die('Something wrong with uploaded file, something missing!'); // output error when above checks fail.
    }

    // Random number will be added after image name
    $RandomNumber   = rand(0, 9999999999); 

    $ImageName      = str_replace(' ','-',strtolower($_FILES['ImageFile']['name'])); //get image name
    $ImageSize      = $_FILES['ImageFile']['size']; // get original image size
    $TempSrc        = $_FILES['ImageFile']['tmp_name']; // Temp name of image file stored in PHP tmp folder
    $ImageType      = $_FILES['ImageFile']['type']; //get file type, returns "image/png", image/jpeg, text/plain etc.

    //Let's check allowed $ImageType, we use PHP SWITCH statement here
    switch(strtolower($ImageType))
    {
        case 'image/png':
            //Create a new image from file 
            $CreatedImage =  imagecreatefrompng($_FILES['ImageFile']['tmp_name']);
            break;
        case 'image/gif':
            $CreatedImage =  imagecreatefromgif($_FILES['ImageFile']['tmp_name']);
            break;          
        case 'image/jpeg':
        case 'image/pjpeg':
            $CreatedImage = imagecreatefromjpeg($_FILES['ImageFile']['tmp_name']);
            break;
        default:
            die('Unsupported File!'); //output error and exit
    }   
    //PHP getimagesize() function returns height/width from image file stored in PHP tmp folder.
    //Get first two values from image, width and height. 
    //list assign svalues to $CurWidth,$CurHeight
    list($CurWidth,$CurHeight)=getimagesize($TempSrc);

    //Get file extension from Image name, this will be added after random name
    $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
    $ImageExt = str_replace('.','',$ImageExt);

    //remove extension from filename
    $ImageName      = preg_replace("/\\.[^.\\s]{3,4}$/", "", $ImageName); 

    //Construct a new name with random number and extension.
    $NewImageName = $RandomNumber.'.'.$ImageExt;

    //set the Destination Image
    $DestRandImageName          = $DestinationDirectory.$NewImageName; // Image with destination directory

    //Resize image to Specified Size by calling resizeImage function.
    if(resizeImage($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType))
    {
        //Create a square Thumbnail right after, this time we are using cropImage() function
        /*
        We have succesfully resized and created thumbnail image
        We can now output image to user's browser or store information in the database
        */
        echo '<table width="100%" border="0" cellpadding="4" cellspacing="0">';
        echo '<tr>';
        echo '</tr><tr>';
        echo '<td align="center"><img src="uploads/'.$NewImageName.'" alt="Resized Image"></td>';
        echo '</tr>';
        echo '</table>';

        /*
        // Insert info into database table!
        mysql_query("INSERT INTO myImageTable (ImageName, ThumbName, ImgPath)
        VALUES ($DestRandImageName, $thumb_DestRandImageName, 'uploads/')");
        */

    }else{
        die('Resize Error'); //output error
    }
}


// This function will proportionally resize image 
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
    //Check Image size is not 0
    if($CurWidth <= 0 || $CurHeight <= 0) 
    {
        return false;
    }

    //Construct a proportional size of new image
    $ImageScale         = min($MaxSize/$CurWidth, $MaxSize/$CurHeight); 
    $NewWidth           = ceil($ImageScale*$CurWidth);
    $NewHeight          = ceil($ImageScale*$CurHeight);
    $NewCanves          = imagecreatetruecolor($NewWidth, $NewHeight);

    //Merci Yves pour la transparence
    imagealphablending($NewCanves, false);
    imagesavealpha($NewCanves,true);
    $transparent = imagecolorallocatealpha($NewCanves, 255, 255, 255, 127);
    imagefilledrectangle($NewCanves, 0, 0, $NewWidth, $NewHeight, $transparent);
    // Resize Image
    if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
    {
        switch(strtolower($ImageType))
        {
            case 'image/png':
                imagepng($NewCanves,$DestFolder);
                break;
            case 'image/gif':
                imagegif($NewCanves,$DestFolder);
                break;          
            case 'image/jpeg':
            case 'image/pjpeg':
                imagejpeg($NewCanves,$DestFolder,$Quality);
                break;
            default:
                return false;
        }
    //Destroy image, frees memory   
    if(is_resource($NewCanves)) {imagedestroy($NewCanves);} 
    return true;
    }

}

//This function corps image to create exact square images, no matter what its original size!
function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{    
    //Check Image size is not 0
    if($CurWidth <= 0 || $CurHeight <= 0) 
    {
        return false;
    }

    //abeautifulsite.net has excellent article about "Cropping an Image to Make Square bit.ly/1gTwXW9
    if($CurWidth>$CurHeight)
    {
        $y_offset = 0;
        $x_offset = ($CurWidth - $CurHeight) / 2;
        $square_size    = $CurWidth - ($x_offset * 2);
    }else{
        $x_offset = 0;
        $y_offset = ($CurHeight - $CurWidth) / 2;
        $square_size = $CurHeight - ($y_offset * 2);
    }

    $NewCanves  = imagecreatetruecolor($iSize, $iSize); 
    if(imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size))
    {
        switch(strtolower($ImageType))
        {
            case 'image/png':
                imagepng($NewCanves,$DestFolder);
                break;
            case 'image/gif':
                imagegif($NewCanves,$DestFolder);
                break;          
            case 'image/jpeg':
            case 'image/pjpeg':
                imagejpeg($NewCanves,$DestFolder,$Quality);
                break;
            default:
                return false;
        }
    //Destroy image, frees memory   
    if(is_resource($NewCanves)) {imagedestroy($NewCanves);} 
    return true;

    }

}

非常感谢

1 个答案:

答案 0 :(得分:0)

解决方案1:

您可以使用ImageMagick调整gif文件的大小而不会丢失动画:

convert animation.gif -coalesce coalesce.gif
convert -size 128x128 coalesce.gif -resize 96x96 small.gif

要在PHP脚本中执行此操作,请使用system()函数。

解决方案2:

如果您不想调整gif动画的大小,您还可以将第一个切换更改为:

switch(strtolower($ImageType))
{
    case 'image/png':
        //Create a new image from file 
        $CreatedImage =  imagecreatefrompng($_FILES['ImageFile']['tmp_name']);
        break;         
    case 'image/jpeg':
    case 'image/pjpeg':
        $CreatedImage = imagecreatefromjpeg($_FILES['ImageFile']['tmp_name']);
        break;
    case 'image/gif':
        $tmp_name = $_FILES['ImageFile']["tmp_name"];
        $name = $_FILES['ImageFile']["name"];
        move_uploaded_file($tmp_name, "$DestinationDirectory/$name");
        break;
    default:
        die('Unsupported File!'); //output error and exit
}

如果您尝试转换gif图像,这将使您的脚本只是将上传的文件移动到目标文件夹。

如果你这样做,你应该在切换后添加这样的东西,以防止在未设置$CreatedImage的情况下执行其余的代码:

if(isset($CreatedImage)){
   // The rest of the code from inside if(isset($_POST)){ ... } 
}