Categories

PHP Image resizing function (PHP/GD2)

by adesignguy Posted on 01/02/2016 19:27:59 | Category: Code

I found this code somewhere online and I am currently using it on this website, but quickly found a problem with transparency when generating GIF or PNG images, it was creating a black background when the images were meant to be transparent, so I have added transparency, after adding the transparency I then discovered another problem on specific images with non standard width/heights showing a black line, so I created a workaround and added a choice of either resizing an image which exists on the server or the ability to handle post data, IE: where you have uploaded a file via a form, and then copying it to a directory, so I added two more arguments to the function to accomplish these caveats.

<?php

// Image resize function with php + gd2 lib
// Usage: image_resize($_FILES['image1'], $img_1thumb,320,240,false,80,false, true);
// or:   image_resize($image, $image,320,240,false,80,false, false);
function image_resize($source, $destination, $width = 0, $height = 0, $crop = false, $quality = 83, $transparency = true, $postdata = true) {
    $quality = $quality ? $quality : 83;
    if ($postdata=true){
    //array type img
    $imgString = file_get_contents($source['tmp_name']); // for use with $_FILES/Post data
    $image = imagecreatefromstring($imgString); 
    } else {
    //disk image file
    $image = imagecreatefromstring($source); //normal type
    }
    if ($image) {
        // Get dimensions
        $w = imagesx($image);
        $h = imagesy($image);
        if (($width && $w > $width) || ($height && $h > $height)) {
            $ratio = $w / $h;
            if (($ratio >= 1 || $height == 0) && $width && !$crop) {
                $new_height = $width / $ratio;
                $new_width = $width;
            } elseif ($crop && $ratio <= ($width / $height)) {
                $new_height = $width / $ratio;
                $new_width = $width;
            } else {
                $new_width = $height * $ratio;
                $new_height = $height;
            }
        } else {
            $new_width = $w;
            $new_height = $h;
        }
        $x_mid = $new_width * 0.5;  //horizontal middle    //orig values were .5
        $y_mid = $new_height * 0.5; //vertical middle      //orig values were .5
        // Resample
        error_log('height: ' . $new_height . ' - width: ' . $new_width);
        $new = imagecreatetruecolor(round($new_width), round($new_height));
       
   //workaround for 1px black line, change it to white rather than black which is better - remove it later
  imagefilledrectangle($new, 0, 0, $new_width, $new_height, imagecolorallocate($new, 255, 255, 255));
 // added to preserve transparency
 
 if ($transparency == true){
  imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
  imagealphablending($new, false);
  imagesavealpha($new, true);
  }
 // added to preserve transparency
 //workaround for 1px line on either bottom or the side of images was to add +2 to the new destination width + height  
        imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width+2, $new_height+2, $w, $h);
        // Crop
        if ($crop) {
            $crop = imagecreatetruecolor($width ? $width : $new_width, $height ? $height : $new_height);
            imagecopyresampled($crop, $new, 0, 0, ($x_mid - ($width * .5)), 0, $width, $height, $width, $height);
            //($y_mid - ($height * .5))
        }
        // Output
        // Enable interlancing [for progressive JPEG]
        imageinterlace($crop ? $crop : $new, true);

        $dext = strtolower(pathinfo($destination, PATHINFO_EXTENSION));
        if ($dext == '') {
            $dext = $ext;
            $destination .= '.' . $ext;
        }
        switch ($dext) {
            case 'jpeg':
            case 'jpg':
                imagejpeg($crop ? $crop : $new, $destination, $quality);
                break;
            case 'png':
                $pngQuality = ($quality - 100) / 11.111111;
                $pngQuality = round(abs($pngQuality));
                imagepng($crop ? $crop : $new, $destination, $pngQuality);
                break;
            case 'gif':
                imagegif($crop ? $crop : $new, $destination);
                break;
        }
        @imagedestroy($image);
        @imagedestroy($new);
        @imagedestroy($crop);
    }
}

?>