How to Resize and Convert an Uploaded Image to a Png Using Gd

How do I resize and convert an uploaded image to a PNG using GD?

<?php                                              
/*
Resizes an image and converts it to PNG returning the PNG data as a string
*/
function imageToPng($srcFile, $maxSize = 100) {
list($width_orig, $height_orig, $type) = getimagesize($srcFile);

// Get the aspect ratio
$ratio_orig = $width_orig / $height_orig;

$width = $maxSize;
$height = $maxSize;

// resize to height (orig is portrait)
if ($ratio_orig < 1) {
$width = $height * $ratio_orig;
}
// resize to width (orig is landscape)
else {
$height = $width / $ratio_orig;
}

// Temporarily increase the memory limit to allow for larger images
ini_set('memory_limit', '32M');

switch ($type)
{
case IMAGETYPE_GIF:
$image = imagecreatefromgif($srcFile);
break;
case IMAGETYPE_JPEG:
$image = imagecreatefromjpeg($srcFile);
break;
case IMAGETYPE_PNG:
$image = imagecreatefrompng($srcFile);
break;
default:
throw new Exception('Unrecognized image type ' . $type);
}

// create a new blank image
$newImage = imagecreatetruecolor($width, $height);

// Copy the old image to the new image
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output to a temp file
$destFile = tempnam();
imagepng($newImage, $destFile);

// Free memory
imagedestroy($newImage);

if ( is_file($destFile) ) {
$f = fopen($destFile, 'rb');
$data = fread($f);
fclose($f);

// Remove the tempfile
unlink($destFile);
return $data;
}

throw new Exception('Image conversion failed.');
}

GD LIB how to convert every uploaded images successfully into jpg type?

If it is not jpg you will fail. Use imagecreatefromstring() instead. This can guess the real format.

Replace the code like this

$img_src = imagecreatefromstring(file_get_contents($oldimage_name));

Resize and Convert all uploading image into png format

Something like http://salman-w.blogspot.com/2008/10/resize-images-using-phpgd-library.html?

Just use png where its says jpeg, like

imagejpeg

etc.

image resize and display using php and gd

You should create two images. One you can create directly from the source

$img_src = imagecreatefrompng($file);

or

$img_src = imagecreatefromjpeg($file);

or

$img_src = imagecreatefromstring(file_get_contents($file));

getting the file size of the src file:

$sizes = imagesize($img_src);
$src_width = $sizes[0];
$src_height = $sizes[1];

But now the image will be scaled to 200x200 even if the src image does not have been same with and height.
You can prevent this by calculating the dst-size:

$faktor = ($src_width > $src_height ? $src_width : $src_height);
$faktor = 100 / $faktor;

$f_width = round($src_width * $faktor);
$f_height = round($src_height * $faktor);

$new_w = 200 * $f_width;
$new_h = 200 * $f_height;

The second one you can create from with your destination size

$img_dest = imagecreatetruecolor($new_w, $new_h);

And then you may copy the resized source to new one

imagecopyresized($img_dest, $img_src, 0, 0, 0, 0, $new_w, $new_h, $src_width, $src_height);
header( "Content-type: image/png" );
imagepng( $img_dest );
imagedestroy( $img_dest );
imagedestroy( $img_src );

P.S.: When creating image from string i think its not good to addslashes to the contents.

Crop/Resize Image Function using GD Library

Here is a class that uses what you have plus a bit more. It will save your file to whatever size dimensions you want. I have notated it for your understanding:

class ImageFactory
{
protected $original;
public $destination;

public function FetchOriginal($file)
{
$size = getimagesize($file);
$this->original['width'] = $size[0];
$this->original['height'] = $size[1];
$this->original['type'] = $size['mime'];
return $this;
}

public function Thumbnailer($thumb_target = '', $width = 60,$height = 60,$SetFileName = false, $quality = 80)
{
// Set original file settings
$this->FetchOriginal($thumb_target);
// Determine kind to extract from
if($this->original['type'] == 'image/gif')
$thumb_img = imagecreatefromgif($thumb_target);
elseif($this->original['type'] == 'image/png') {
$thumb_img = imagecreatefrompng($thumb_target);
$quality = 7;
}
elseif($this->original['type'] == 'image/jpeg')
$thumb_img = imagecreatefromjpeg($thumb_target);
else
return false;
// Assign variables for calculations
$w = $this->original['width'];
$h = $this->original['height'];
// Calculate proportional height/width
if($w > $h) {
$new_height = $height;
$new_width = floor($w * ($new_height / $h));
$crop_x = ceil(($w - $h) / 2);
$crop_y = 0;
}
else {
$new_width = $width;
$new_height = floor( $h * ( $new_width / $w ));
$crop_x = 0;
$crop_y = ceil(($h - $w) / 2);
}
// New image
$tmp_img = imagecreatetruecolor($width,$height);
// Copy/crop action
imagecopyresampled($tmp_img, $thumb_img, 0, 0, $crop_x, $crop_y, $new_width, $new_height, $w, $h);
// If false, send browser header for output to browser window
if($SetFileName == false)
header('Content-Type: '.$this->original['type']);
// Output proper image type
if($this->original['type'] == 'image/gif')
imagegif($tmp_img);
elseif($this->original['type'] == 'image/png')
($SetFileName !== false)? imagepng($tmp_img, $SetFileName, $quality) : imagepng($tmp_img);
elseif($this->original['type'] == 'image/jpeg')
($SetFileName !== false)? imagejpeg($tmp_img, $SetFileName, $quality) : imagejpeg($tmp_img);
// Destroy set images
if(isset($thumb_img))
imagedestroy($thumb_img);
// Destroy image
if(isset($tmp_img))
imagedestroy($tmp_img);
}
}

Example of usage:

// Initiate class
$ImageMaker = new ImageFactory();

// Here is just a test landscape sized image
$thumb_target = 'http://media1.santabanta.com/full6/Outdoors/Landscapes/landscapes-246a.jpg';

// This will save the file to disk. $destination is where the file will save and with what name
$destination = 'image60px.jpg';
$ImageMaker->Thumbnailer($thumb_target,120,120,$destination);

// This example will just display to browser, not save to disk
// $ImageMaker->Thumbnailer($thumb_target,120,120);


Related Topics



Leave a reply



Submit