I have recently created an upload function, but I don\'t know how to change the width and height to 75px... I tried one code I found through Google, but I just got this erro
Simple solutions some times changed alfa (transparent png). For me forks only this (it's real, just copy-pasted code)
public static function resize($filename, $maxW, $maxH, $ext = null)
{
if ($ext === null)
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
else
$ext = strtolower($ext);
list($origW, $origH) = getimagesize($filename);
$w = $origW;
$h = $origH;
# taller
if ($h > $maxH) {
$w = ($maxH / $h) * $w;
$h = $maxH;
}
# wider
if ($w > $maxW) {
$h = ($maxW / $w) * $h;
$w = $maxW;
}
$resource = imagecreatetruecolor($w, $h);
# MUST IMPORTANT TWO ROWS
imagesavealpha($resource, true);
imagefill($resource, 0, 0, 0x7fffffff);
if ($ext == 'jpeg' || $ext == 'jpg') {
$image = imagecreatefromjpeg($filename);
} else if ($ext == 'png') {
$image = imagecreatefrompng($filename);
} else {
throw new Exception('Unsupported extension');
}
imagecopyresampled(
$resource,
$image, 0, 0, 0, 0,
$w, $h, $origW, $origH
);
imagedestroy($image);
return $resource;
}
The Imagick Class is not found, because it is a PHP extension you need to install on your server.
Read the following documentation to find instructions on how to use/install the extension. http://www.php.net/manual/en/book.imagick.php
You can also scale the image client-side, e.g. using plupload library (can resize JPEG and PNG).
I've successfully used GD to do this recently, specifically using the imagecopyresampled
function.
To expand a little on that... Once I had the image uploaded (which I wont go into, because that is a whole other issue), I did something fairly simple like this:
$original_info = getimagesize($filename);
$original_w = $original_info[0];
$original_h = $original_info[1];
$original_img = imagecreatefromjpg($filename);
$thumb_w = 100;
$thumb_h = 100;
$thumb_img = imagecreatetruecolor($thumb_w, $thumb_h);
imagecopyresampled($thumb_img, $original_img,
0, 0,
0, 0,
$thumb_w, $thumb_h,
$original_w, $original_h);
imagejpeg($thumb_img, $thumb_filename);
imagedestroy($thumb_img);
imagedestroy($original_img);
Please note that I have not tested this code. It's just here to give you a basic idea of my method.
I use code like this:
$t = imagecreatefromjpeg($old_path);
$x = imagesx($t);
$y = imagesy($t);
$s = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($s, $t, 0, 0, 0, 0, $new_width, $new_height,
$x, $y);
imagejpeg($s, $new_path);
chmod($new_path, 0644);