PHP: Crop image function (part 2)


Read first: PHP: Crop image function

We can save to new file:

<?php
set_time_limit(0);

function createblankpng($w, $h)
{
    $im = imagecreatetruecolor($w, $h);
    imagesavealpha($im, true);
    $transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
    imagefill($im, 0, 0, $transparent);
    return $im;
}

function image_crop($file_loc, $x, $y, $w, $h, $new_file = false)
{
    $info = getimagesize($file_loc);
    switch ($info[2]) {
        case 1: //gif image
            $image = imagecreatefromgif($file_loc);
            break;
        case 2: //jpeg  image
            $image = imagecreatefromjpeg($file_loc);
            break;
        case 3: //png  image
            $image = imagecreatefrompng($file_loc);
            break;
        default:
            trigger_error('Support GIF JPEG PNG only', E_USER_ERROR);
            break;
    }

    $crop = createblankpng($w, $h);
    imagecopy($crop, $image, 0, 0, $x, $y, $w, $h);


    if ($new_file === false) {
        header("Content-Type: image/png");
        imagepng($crop);
    } else
        imagepng($crop, $new_file, 0);
    imagedestroy($image);
    imagedestroy($crop);

}

$file_loc = 'http://tutorialspots.com/wp-content/uploads/2012/08/logo1.png';
$new_file = 'logo.png';

image_crop($file_loc, 8, 26, 270, 60, $new_file);
?>

Online demo

We get the result: http://www.demo.tutorialspots.com/crop/logo.png

Now, we can crop image on the fly:
Source code of file crop.php

<?php
$w = $_GET['w'];
$h = isset($_GET['h']) ? $_GET['h'] : $w; 
$x = isset($_GET['x']) ? $_GET['x'] : 0; 
$y = isset($_GET['y']) ? $_GET['y'] : 0; 
$filename = $_GET['src'];

function createblankpng($w, $h)
{
    $im = imagecreatetruecolor($w, $h);
    imagesavealpha($im, true);
    $transparent = imagecolorallocatealpha($im, 0, 0, 0, 127);
    imagefill($im, 0, 0, $transparent);
    return $im;
}

function image_crop($file_loc, $x, $y, $w, $h, $new_file = false)
{
    $info = getimagesize($file_loc);
    switch ($info[2]) {
        case 1: //gif image
            $image = imagecreatefromgif($file_loc);
            break;
        case 2: //jpeg  image
            $image = imagecreatefromjpeg($file_loc);
            break;
        case 3: //png  image
            $image = imagecreatefrompng($file_loc);
            break;
        default:
            trigger_error('Support GIF JPEG PNG only', E_USER_ERROR);
            break;
    }

    $crop = createblankpng($w, $h);
    imagecopy($crop, $image, 0, 0, $x, $y, $w, $h);


    if ($new_file === false) {
        header("Content-Type: image/png");
        imagepng($crop);
    } else
        imagepng($crop, $new_file, 0);
    imagedestroy($image);
    imagedestroy($crop);

}

image_crop($filename, $x, $y, $w, $h);
?>

Example 1: PHP logo

Source picture

http://www.demo.tutorialspots.com/crop/crop.php?src=http://static.php.net/www.php.net/images/php.gif&w=80&h=40&x=20&y=13

we get the result:

Example 2:
Source picture http://incloak.com/images/proxylist_port_4084543.gif

http://www.demo.tutorialspots.com/crop/crop.php?w=5&h=8&x=0&y=3&src=http://incloak.com/images/proxylist_port_4084543.gif

we get the result:

Continuing…

Leave a Reply