<?php // Plug-in 18: Image Convert
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link
// You will need an image file called photo.jpg in this folder
PIPHP_ImageConvert("photo.jpg", "photo4.png", "png", 0);
echo "A 42Kb jpeg converted to another jpeg of 8Kb (20% of ";
echo "original) at a quality setting of 25...<br />";
echo "<img src='photo.jpg' align='left'> <img src='photo4.png'>";
function PIPHP_ImageConvert($fromfile, $tofile, $type, $quality)
{
// Plug-in 18: Image Convert
//
// This plug-in takes an image file name, then loads,
// converts and saves it as a new file. The Arguments
// required are:
//
// $fromfile: Path and/or name of image to convert from
// $tofile: Path and/or name of image to convert to
//
// $type: Type of file to convert to:
//
// "gif": Convert to a gif image
// "jpeg": Convert to a jpeg image
// "png": Convert to a png image
//
// $quality: Quality of image (0-99).
// Only used if $type is jpeg or png
//
// 0 = Lowest quality and smallest size
// 99 = Best quality and largest size
$contents = file_get_contents($fromfile);
$image = imagecreatefromstring($contents);
switch($type)
{
case "gif": imagegif($image, $tofile); break;
case "jpeg": imagejpeg($image, $tofile, $quality); break;
case "png": imagepng($image, $tofile,
round(9 - $quality * .09)); break;
}
}
?>
插件说明:
插件18需要输入一个图像文件名、转换后要保存的文件名以及图像质量。具体如下:
$fromfile 字符串,表示图像文件的路径和文件名。
$tofile 字符串,表示新图像保存的路径和文件名。
$type 图像文件的类型(如GIF,JPEG或PNG).
$quality 表示JPEG或PNG图像的质量(0表示最低质量,99表示最高质量)。