Create a thumbnail
{
// Open the original image.
$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original");
list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
// Resample the image.
$tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");
// Create the new file name.
$newNameE = explode(".", $img);
$newName = ”. $newNameE[0] .”. $suffix .‘.’. $newNameE[1] .”;
// Save the image.
imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image");
// Clean up.
imagedestroy($original);
imagedestroy($tempImg);
return true;
}
?>
Rotate
function rotateImage($img, $imgPath, $suffix, $degrees, $quality, $save)
{
// Open the original image.
$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original");
list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
// Resample the image.
$tempImg = imagecreatetruecolor($width, $height) or die("Cant create temp image");
imagecopyresized($tempImg, $original, 0, 0, 0, 0, $width, $height, $width, $height) or die("Cant resize copy");
// Rotate the image.
$rotate = imagerotate($original, $degrees, 0);
// Save.
if($save)
{
// Create the new file name.
$newNameE = explode(".", $img);
$newName = ”. $newNameE[0] .”. $suffix .‘.’. $newNameE[1] .”;
// Save the image.
imagejpeg($rotate, "$imgPath/$newName", $quality) or die("Cant save image");
}
// Clean up.
imagedestroy($original);
imagedestroy($tempImg);
return true;
}
?>
Resize
function resizeImage($img, $imgPath, $suffix, $by, $quality)
{
// Open the original image.
$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original ($imgPath/$img)");
list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
// Determine new width and height.
$newWidth = ($width/$by);
$newHeight = ($height/$by);
// Resample the image.
$tempImg = imagecreatetruecolor($newWidth, $newHeight) or die("Cant create temp image");
imagecopyresized($tempImg, $original, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height) or die("Cant resize copy");
// Create the new file name.
$newNameE = explode(".", $img);
$newName = ”. $newNameE[0] .”. $suffix .‘.’. $newNameE[1] .”;
// Save the image.
imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image");
// Clean up.
imagedestroy($original);
imagedestroy($tempImg);
return true;
}
?>
Reduce
function reduceImage($img, $imgPath, $suffix, $quality)
{
// Open the original image.
$original = imagecreatefromjpeg("$imgPath/$img") or die("Error Opening original ($imgPath/$img)");
list($width, $height, $type, $attr) = getimagesize("$imgPath/$img");
// Resample the image.
$tempImg = imagecreatetruecolor($width, $height) or die("Cant create temp image");
imagecopyresized($tempImg, $original, 0, 0, 0, 0, $width, $height, $width, $height) or die("Cant resize copy");
// Create the new file name.
$newNameE = explode(".", $img);
$newName = ”. $newNameE[0] .”. $suffix .‘.’. $newNameE[1] .”;
// Save the image.
imagejpeg($tempImg, "$imgPath/$newName", $quality) or die("Cant save image");
// Clean up.
imagedestroy($original);
imagedestroy($tempImg);
return true;
}
?>
The simple instructions I used for the examples
$rotate = rotateImage($img, $imgPath, "-rotated", 270, 100, 1);
$resize = resizeImage($img, $imgPath, "-resized", 2, 100);
$reduce = reduceImage($img, $imgPath, "-reduced", 70);
?>
Those are the function calls i used to create the images linked to below.
createThumbnail variables
image file
image file path
suffix to append to the name before the .jpg
thumbnail width
thumbnail height
thumbnail quality
rotateImage variables
image file
image file path
suffix to append to the name before the .jpg
degrees to rotate (anticlockwise i think) by
image quality
save or not (if you wanted to output to a temp to preview, i dont use it, so leave as 1
resize variables
image file
image file path
suffix to append to the name before the .jpg
resize by (i used 2 which i thought was half.. not so hot on the maths)
image quality
reduce variables
image file
image file path
suffix to append to the name before the .jpg
quality (100 best, 0 worst… Duhh).
Here are the links to my examples
Original File (904k (1840×1232))
Thumbnail (16k (120×100))
Resized and Reduced (24k (368×246))
ref: http://nodstrum.com/2006/12/09/image-manipulation-using-php/#more-7
0 回覆:
發佈留言