Search code examples
phppngjpegcropjcrop

How do I allow PNG's in my crop script?


I've got an upload function that allows both Jpegs and PNG's. After the image is uploaded, a php file is called that handles a crop function to crop the uploaded image.

After the image is cropped, it is once again saved to the server under a new name. In this current setup, only jpegs are able to be written to the server. Anything else will draw a black image. I was wondering How I write this code so that the crop also allows PNG's

The code that handles the crop:

$imageLocation = $_SESSION['image_location'];
$newNameOverwrite = $_SESSION['new_name'];

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$jpeg_quality = 100;

$src = $imageLocation;
list($width, $height, $type, $attr) = getimagesize($src);
$targ_w = $width;
$targ_h = $height;
$img_r = imagecreatefromjpeg($src);
$dst_r = imagecreatetruecolor($_POST[('w')], $_POST[('h')]);
$uploadLocation = 'uploads/';
$name = $uploadLocation.'resized_'.$newNameOverwrite;

imagecopy(
$dst_r, $img_r,
0, 0, $_POST['x'], $_POST['y'],
$_POST['w'], $_POST['h']
);

imagejpeg($dst_r,$name,100); 

$imageCropped = $name;
$_SESSION['image_cropped'] = $imageCropped;

//Thumbnail generate
include('SimpleImage.php');
$imageThumbnail = new SimpleImage();
$imageThumbnail->load($name);
$imageThumbnail->resizeToWidth(200);
$imageThumbnail->save($uploadLocation.'resizedThumbnail_'.$newNameOverwrite);

$imageThumbnailCropped = ($uploadLocation.'resizedThumbnail_'.$newNameOverwrite);

$imageThumbnailCroppedSession = $imageThumbnailCropped;
$_SESSION['image_cropped_thumbnail'] = $imageThumbnailCroppedSession;
}

Updated code:

$imageType = $_SESSION['image_type'];

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$jpeg_quality = 100;

$src = $imageLocation;
list($width, $height, $type, $attr) = getimagesize($src);
$targ_w = $width;
$targ_h = $height;
    if ($imageType == '.jpg' || $imageType == '.jpeg'){
        $img_r = imagecreatefromjpeg($src);
    }
    if ($imageType == '.png'){
        $img_r = imagecreatefrompng($src);
    }       

$dst_r = imagecreatetruecolor($_POST[('w')], $_POST[('h')]);
$uploadLocation = 'uploads/';
$name = $uploadLocation.'resized_'.$newNameOverwrite; 

imagecopy(
$dst_r, $img_r,
0, 0, $_POST['x'], $_POST['y'],
$_POST['w'], $_POST['h']
);
var_dump($imageType);
if ($imageType == '.png'){
    imagepng($dst_r,$name); 
}
if ($imageType == '.jpg' || $imageType == '.jpeg'){
    imagejpeg($dst_r,$name, 100); 
    }   
$imageCropped = $name;
$_SESSION['image_cropped'] = $imageCropped;


include('SimpleImage.php');
$imageThumbnail = new SimpleImage();
$imageThumbnail->load($name);
$imageThumbnail->resizeToWidth(200);
$imageThumbnail->save($uploadLocation.'resizedThumbnail_'.$newNameOverwrite);

$imageThumbnailCropped = ($uploadLocation.'resizedThumbnail_'.$newNameOverwrite);

$imageThumbnailCroppedSession = $imageThumbnailCropped;
$_SESSION['image_cropped_thumbnail'] = $imageThumbnailCroppedSession;
}

Solution

  • Use PHP to determine what type of image it is, then dynamically use *_png functions instead of *_jpeg ones. IE, instead of imagecreatefromjpeg use imagecreatefrompng