开发者

How to resize an image using PEAR in PHP

Could anyon开发者_JAVA百科e please provide me an example which is very easy to understand to resize an image using PEAR in PHP...

Thanks in advance...


Using Image_Transform package:

http://pear.php.net/manual/en/package.images.image-transform.general.php1


You are looking for the Image_Transform package from PEAR. The relevant manual page is at http://pear.php.net/manual/en/package.images.image-transform.scaling.php

Considering you are explicitly looking for a pear package to do this work, I presume you already know how to install image_transform. It is as easy as:

$ sudo pear install image_transform-0.9.3

One example of using the package:

<?php
require_once 'Image/Transform.php';

// factory pattern - returns an object
$a = Image_Transform::factory('GD');

// load the image file
$a->load("teste.jpg");

// scale image by percentage - 40% of its original size
$a->scalebyPercentage(40);

// displays the image
$a->display();
?>

and another example:

<?php
require_once 'Image/Transform.php';
$it = Image_Transform::factory("IM");
$it->load("image.png");
$it->resize(2,2);
$it->save("resized.png");
?>

Other examples, provided in the package can be found by doing: $ pear list image_transform


You can make use of imagecopyresampled function as:

Sample program (source: php.net)

<?php

// Image source.
$filename = 'http://valplibrary.files.wordpress.com/2009/01/5b585d_merry-christmas-blue-style.jpg';

$percent = 0.5; // percentage of resize

// send header with correct MIME.
header('Content-type: image/jpeg');

// Get image dimensions
list($width, $height) = getimagesize($filename);

// compute new dimensions.
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output the resized image.
imagejpeg($image_p, null, 100);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜