Pliki graficzne – małe kompedium skryptów.
Luty 26th, 2010 • Bez kategorii
Sieć przepełniona jest różnymi obrazami, zdjęciami, miniaturkami i wszelkiego rodzaju grafiką rastrową, co wymaga od nas – programistów – skryptów zmniejszających, ucinających, modyfikujących te pliki. Zebraliśmy kilka klas i skryptów, które zdecydowanie ułatwiają pracę z obrazkami.
Resize an Image (on the fly) & Keep its Aspect Ratio using PHP and GD
<?php
/*
---------------------------------------------------------------------
Credits: Bit Repository
Source URL: http://www.bitrepository.com/resize-an-image-keeping-its-aspect-ratio-using-php-and-gd.html
---------------------------------------------------------------------
*/
class Resize_Image {
var $image_to_resize;
var $new_width;
var $new_height;
var $ratio;
var $new_image_name;
var $save_folder;
function resize()
{
if(!file_exists($this->image_to_resize))
{
exit("File ".$this->image_to_resize." does not exist.");
}
$info = GetImageSize($this->image_to_resize);
if(empty($info))
{
exit("The file ".$this->image_to_resize." doesn't seem to be an image.");
}
$width = $info[0];
$height = $info[1];
$mime = $info['mime'];
/*
Keep Aspect Ratio?
Improved, thanks to Larry
*/
if($this->ratio)
{
// if preserving the ratio, only new width or new height
// is used in the computation. if both
// are set, use width
if (isset($this->new_width))
{
$factor = (float)$this->new_width / (float)$width;
$this->new_height = $factor * $height;
}
else if (isset($this->new_height))
{
$factor = (float)$this->new_height / (float)$height;
$this->new_width = $factor * $width;
}
else
exit(â€neither new height or new width has been setâ€);
}
// What sort of image?
$type = substr(strrchr($mime, '/'), 1);
switch ($type)
{
case 'jpeg':
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
break;
case 'png':
$image_create_func = 'ImageCreateFromPNG';
$image_save_func = 'ImagePNG';
$new_image_ext = 'png';
break;
case 'bmp':
$image_create_func = 'ImageCreateFromBMP';
$image_save_func = 'ImageBMP';
$new_image_ext = 'bmp';
break;
case 'gif':
$image_create_func = 'ImageCreateFromGIF';
$image_save_func = 'ImageGIF';
$new_image_ext = 'gif';
break;
case 'vnd.wap.wbmp':
$image_create_func = 'ImageCreateFromWBMP';
$image_save_func = 'ImageWBMP';
$new_image_ext = 'bmp';
break;
case 'xbm':
$image_create_func = 'ImageCreateFromXBM';
$image_save_func = 'ImageXBM';
$new_image_ext = 'xbm';
break;
default:
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
}
// New Image
$image_c = ImageCreateTrueColor($this->new_width, $this->new_height);
$new_image = $image_create_func($this->image_to_resize);
ImageCopyResampled($image_c, $new_image, 0, 0, 0, 0, $this->new_width, $this->new_height, $width, $height);
if($this->save_folder)
{
if($this->new_image_name)
{
$new_name = $this->new_image_name.'.'.$new_image_ext;
}
else
{
$new_name = $this->new_thumb_name( basename($this->image_to_resize) ).'_resized.'.$new_image_ext;
}
$save_path = $this->save_folder.$new_name;
}
else
{
/* Show the image without saving it to a folder */
header("Content-Type: ".$mime);
$image_save_func($image_c);
$save_path = '';
}
$process = $image_save_func($image_c, $save_path);
return array('result' => $process, 'new_file_path' => $save_path);
}
function new_thumb_name($filename)
{
$string = trim($filename);
$string = strtolower($string);
$string = trim(ereg_replace("[^ A-Za-z0-9_]", " ", $string));
$string = ereg_replace("[ tnr]+", "_", $string);
$string = str_replace(" ", '_', $string);
$string = ereg_replace("[ _]+", "_", $string);
return $string;
}
}
?>
Użycie:
<?php
include 'resize.image.class.php';
$image = new Resize_Image;
$image->new_width = 200;
$image->new_height = 200;
$image->image_to_resize = "/home/mysite.com/public_html/images/sunset_wallpaper.jpg"; // Full Path to the file
$image->ratio = true; // Keep Aspect Ratio?
// Name of the new image (optional) - If it's not set a new will be added automatically
$image->new_image_name = 'sunset_wallpaper_thumbnail';
/* Path where the new image should be saved. If it's not set the script will output the image without saving it */
$image->save_folder = 'thumbs/';
$process = $image->resize();
if($process['result'] && $image->save_folder)
{
echo 'The new image ('.$process['new_file_path'].') has been saved.';
}
?>
Cropping a Rectangle Image to Square using GD
<?php
/*
--------------------------------------------------------------------------------------------
Credits: Bit Repository
Source URL: http://www.bitrepository.com/web-programming/php/crop-rectangle-to-square.html
--------------------------------------------------------------------------------------------
*/
/* Crop Image Class */
class Crop_Image_To_Square {
var $source_image;
var $new_image_name;
var $save_to_folder;
function crop($location = 'center')
{
$info = GetImageSize($this->source_image);
$width = $info[0];
$height = $info[1];
$mime = $info['mime'];
if($width == $height)
{
echo 'The source image is already a square.';
}
else
{
// What sort of image?
$type = substr(strrchr($mime, '/'), 1);
switch ($type)
{
case 'jpeg':
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
break;
case 'png':
$image_create_func = 'ImageCreateFromPNG';
$image_save_func = 'ImagePNG';
$new_image_ext = 'png';
break;
case 'bmp':
$image_create_func = 'ImageCreateFromBMP';
$image_save_func = 'ImageBMP';
$new_image_ext = 'bmp';
break;
case 'gif':
$image_create_func = 'ImageCreateFromGIF';
$image_save_func = 'ImageGIF';
$new_image_ext = 'gif';
break;
case 'vnd.wap.wbmp':
$image_create_func = 'ImageCreateFromWBMP';
$image_save_func = 'ImageWBMP';
$new_image_ext = 'bmp';
break;
case 'xbm':
$image_create_func = 'ImageCreateFromXBM';
$image_save_func = 'ImageXBM';
$new_image_ext = 'xbm';
break;
default:
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
}
// Coordinates calculator
if($width > $height) // Horizontal Rectangle?
{
if($location == 'center')
{
$x_pos = ($width - $height) / 2;
$x_pos = ceil($x_pos);
$y_pos = 0;
}
else if($location == 'left')
{
$x_pos = 0;
$y_pos = 0;
}
else if($location == 'right')
{
$x_pos = ($width - $height);
$y_pos = 0;
}
$new_width = $height;
$new_height = $height;
}
else if($height > $width) // Vertical Rectangle?
{
if($location == 'center')
{
$x_pos = 0;
$y_pos = ($height - $width) / 2;
$y_pos = ceil($y_pos);
}
else if($location == 'left')
{
$x_pos = 0;
$y_pos = 0;
}
else if($location == 'right')
{
$x_pos = 0;
$y_pos = ($height - $width);
}
$new_width = $width;
$new_height = $width;
}
$image = $image_create_func($this->source_image);
$new_image = ImageCreateTrueColor($new_width, $new_height);
// Crop to Square using the given dimensions
ImageCopy($new_image, $image, 0, 0, $x_pos, $y_pos, $width, $height);
if($this->save_to_folder)
{
if($this->new_image_name)
{
$new_name = $this->new_image_name.'.'.$new_image_ext;
}
else
{
$new_name = $this->new_image_name( basename($this->source_image) ).'_square_'.$location.'.'.$new_image_ext;
}
$save_path = $this->save_to_folder.$new_name;
}
else
{
/* Show the image (on the fly) without saving it to a folder */
header("Content-Type: ".$mime);
$image_save_func($new_image);
$save_path = '';
}
// Save image
$process = $image_save_func($new_image, $save_path) or die("There was a problem in saving the new file.");
return array('result' => $process, 'new_file_path' => $save_path);
}
}
function new_image_name($filename)
{
$string = trim($filename);
$string = strtolower($string);
$string = trim(ereg_replace("[^ A-Za-z0-9_]", " ", $string));
$string = ereg_replace("[ \t\n\r]+", "_", $string);
$string = str_replace(" ", '_', $string);
$string = ereg_replace("[ _]+", "_", $string);
return $string;
}
}
?>
Oraz sposób użycia:
<?php
include 'crop.image.to.square.class.php';
$crop = new Crop_Image_To_Square;
$crop->source_image = 'my_rectangle_image.jpg';
$crop->save_to_folder = 'square_images/';
/* left, center or right; If none is set, center will be used as default */
$process = $crop->crop('right');
if($process['result'])
{
echo 'The rectangle image (<em>'.$process['new_file_path'].'</em>) was cropped.';
}
?>
How to Create Mirror Images using GD
<?php
/*
--------------------------------------------------------------------------------------------
Credits: Bit Repository
Source URL: http://www.bitrepository.com/
--------------------------------------------------------------------------------------------
*/
/* Image Mirror Class */
class Image_Mirror {
var $source_image;
var $new_image_name;
var $save_to_folder;
function make_mirror_image($flip = 1)
{
$info = GetImageSize($this->source_image);
if(empty($info)) {
exit("The file from the requested path doesn't see to be an image");
}
$width = $info[0];
$height = $info[1];
$mime = $info['mime'];
// What sort of image?
$type = substr(strrchr($mime, '/'), 1);
switch ($type)
{
case 'jpeg':
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
$quality = 100; // best quality
break;
case 'png':
$image_create_func = 'ImageCreateFromPNG';
$image_save_func = 'ImagePNG';
$new_image_ext = 'png';
$quality = 0; // no compression
break;
case 'bmp':
$image_create_func = 'ImageCreateFromBMP';
$image_save_func = 'ImageBMP';
$new_image_ext = 'bmp';
break;
case 'gif':
$image_create_func = 'ImageCreateFromGIF';
$image_save_func = 'ImageGIF';
$new_image_ext = 'gif';
break;
case 'vnd.wap.wbmp':
$image_create_func = 'ImageCreateFromWBMP';
$image_save_func = 'ImageWBMP';
$new_image_ext = 'bmp';
break;
case 'xbm':
$image_create_func = 'ImageCreateFromXBM';
$image_save_func = 'ImageXBM';
$new_image_ext = 'xbm';
break;
default:
$image_create_func = 'ImageCreateFromJPEG';
$image_save_func = 'ImageJPEG';
$new_image_ext = 'jpg';
}
// Source Image
$image = $image_create_func($this->source_image);
$new_image = ImageCreateTrueColor($width, $height);
// Set a White & Transparent Background Color (PHP 4 >= 4.3.2, PHP 5)
$bg = ImageColorAllocateAlpha($new_image, 255, 255, 255, 127);
ImageFill($new_image, 0, 0 , $bg);
if($flip == 1)
{
$dst_y = 0;
$src_y = 0;
$coordinate = ($width - 1);
foreach(range($width, 0) as $range)
{
$src_x = $range;
$dst_x = $coordinate - $range;
ImageCopy($new_image, $image, $dst_x, $dst_y, $src_x, $src_y, 1, $height);
}
}
elseif($flip == 2)
{
$dst_x = 0;
$src_x = 0;
$coordinate = ($height - 1);
foreach(range($height, 0) as $range)
{
$src_y = $range;
$dst_y = $coordinate - $range;
ImageCopy($new_image, $image, $dst_x, $dst_y, $src_x, $src_y, $width, 1);
}
}
if(isSet($this->save_to_folder))
{
if($this->new_image_name)
{
$new_name = $this->new_image_name.'.'.$new_image_ext;
}
else
{
$basename = basename($this->source_image);
$new_name = $this->new_image_name($basename).'_mirror.'.$new_image_ext;
}
$save_path = $this->save_to_folder.$new_name;
}
else
{
/* Set the right header for the image */
header("Content-Type: ".$mime);
$save_path = '';
}
// Show/Save image
if(isSet($quality))
{
$process = $image_save_func($new_image, $save_path, $quality);
}
else
{
$process = $save_path ? $image_save_func($new_image, $save_path) : $image_save_func($new_image);
}
return array('result' => $process, 'new_file_path' => $save_path);
}
function new_image_name($filename)
{
$ext = strrchr($filename, ".");
if($ext)
{
$strlen = strlen($ext);
$filename = basename(substr($filename, 0, -$strlen));
}
$string = trim($filename);
$string = strtolower($string);
$string = trim(ereg_replace("[^ A-Za-z0-9_]", " ", $string));
$string = ereg_replace("[ \t\n\r]+", "_", $string);
$string = str_replace(" ", '_', $string);
$string = ereg_replace("[ _]+", "_", $string);
return $string;
}
}
?>
Całość domyka: http://www.bitrepository.com/image-cropping-with-jquery-mootools-prototype-scriptaculous.html