| 1 |
<?php
|
| 2 |
/* $Id: image_manip.inc,v 1.8 2006/09/14 22:09:01 vhmauery Exp $ */
|
| 3 |
|
| 4 |
/*
|
| 5 |
Acidfree Photo Albums for Drupal
|
| 6 |
Copyright (C) 2005 Vernon Mauery
|
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify
|
| 9 |
it under the terms of the GNU General Public License as published by
|
| 10 |
the Free Software Foundation; either version 2 of the License, or
|
| 11 |
(at your option) any later version.
|
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful,
|
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 16 |
GNU General Public License for more details.
|
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License
|
| 19 |
along with this program; if not, write to the Free Software
|
| 20 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
| 21 |
*/
|
| 22 |
|
| 23 |
/** @file
|
| 24 |
* this file contains functions to abstract the image functions to make it
|
| 25 |
* easier for us to make the calls the way we want.
|
| 26 |
*/
|
| 27 |
|
| 28 |
/**
|
| 29 |
* create a temporary file in the drupal temp dir
|
| 30 |
*/
|
| 31 |
function _image_manip_mktmpfile($name, $ext=false) {
|
| 32 |
$tmp = file_directory_temp();
|
| 33 |
$file = tempnam($tmp, $name);
|
| 34 |
if ($ext) {
|
| 35 |
rename($file, "{$file}.{$ext}");
|
| 36 |
return "{$file}.{$ext}";
|
| 37 |
}
|
| 38 |
return $file;
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* resize an image using the image library of choice
|
| 43 |
*
|
| 44 |
* @param $large
|
| 45 |
* path to a full size image to be resized
|
| 46 |
* @param $small
|
| 47 |
* path to a smaller image that will be created
|
| 48 |
* if $small == $large, the $large image will be replaced
|
| 49 |
* @param $size
|
| 50 |
* largest dimension for the new image
|
| 51 |
*
|
| 52 |
* @return
|
| 53 |
* false if image was not resized (not image file or size is smaller than image)
|
| 54 |
* true if image was resized as requested
|
| 55 |
*/
|
| 56 |
function acidfree_resize_image($large, $small, $x, $y=0) {
|
| 57 |
if ($large == $small) {
|
| 58 |
$small = _image_manip_mktmpfile('imagemanip', _acidfree_ext_from_file($small));
|
| 59 |
$tmp = true;
|
| 60 |
}
|
| 61 |
if ($y==0) {
|
| 62 |
$y = $x;
|
| 63 |
}
|
| 64 |
if (image_scale($large, $small, $x, $y)) {
|
| 65 |
if ($tmp) {
|
| 66 |
file_move($small, $large, FILE_EXISTS_REPLACE);
|
| 67 |
}
|
| 68 |
return true;
|
| 69 |
} else {
|
| 70 |
if ($tmp) {
|
| 71 |
file_delete($small);
|
| 72 |
}
|
| 73 |
return false;
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
| 77 |
/**
|
| 78 |
* rotate an image the best we can
|
| 79 |
*
|
| 80 |
* @param $image
|
| 81 |
* path to the image to be rotated
|
| 82 |
* @param $angle
|
| 83 |
* angle to rotate the image. If this is 90, 180, or 270,
|
| 84 |
* we will first attempt lossless rotation. If that fails,
|
| 85 |
* we will use lossy rotation. If this is any other angle,
|
| 86 |
* we will skip straight to lossy rotation using convert.
|
| 87 |
*
|
| 88 |
* @return
|
| 89 |
* true for success, false for failure
|
| 90 |
*/
|
| 91 |
function acidfree_rotate_image($image, $angle) {
|
| 92 |
$ext = _acidfree_ext_from_file($image);
|
| 93 |
if (($angle == 90 || $angle ==180 || $angle == 270 || $angle == 'auto') && $ext == 'jpg') {
|
| 94 |
// try to do lossless rotation first
|
| 95 |
$exiftran_path = variable_get('acidfree_path_to_exiftran', '/usr/bin/exiftran');
|
| 96 |
if (is_executable($exiftran_path)) {
|
| 97 |
$command = "{$exiftran_path} -i -{$angle{0}} $image";
|
| 98 |
system($command, $ret);
|
| 99 |
return true;
|
| 100 |
} else if ($angle == 'auto') {
|
| 101 |
drupal_set_message('you must have exiftran installed to use automatic rotation', 'error');
|
| 102 |
return false;
|
| 103 |
}
|
| 104 |
$jpegtran_path = variable_get('acidfree_path_to_jpegtran', '/usr/bin/jpegtran');
|
| 105 |
if (is_executable($jpegtran_path)) {
|
| 106 |
$outfile = _image_manip_mktmpfile('imagemanip', _acidfree_ext_from_file($image));
|
| 107 |
$command = "{$jpegtran_path} -copy all -rotate $angle -outfile $outfile $image";
|
| 108 |
system($command, $ret);
|
| 109 |
if (!$ret) {
|
| 110 |
file_move($outfile, $image, FILE_EXISTS_REPLACE);
|
| 111 |
return true;
|
| 112 |
}
|
| 113 |
file_delete($outfile);
|
| 114 |
return false;
|
| 115 |
}
|
| 116 |
}
|
| 117 |
$outfile = _image_manip_mktmpfile('imagemanip', _acidfree_ext_from_file($image));
|
| 118 |
if (image_rotate($image, $outfile, $angle)) {
|
| 119 |
file_move($outfile, $image, FILE_EXISTS_REPLACE);
|
| 120 |
} else {
|
| 121 |
drupal_set_message(t('image rotate failed for %image', array('%image' => $image)));
|
| 122 |
file_delete($outfile);
|
| 123 |
return false;
|
| 124 |
}
|
| 125 |
return true;
|
| 126 |
}
|
| 127 |
|
| 128 |
/**
|
| 129 |
* composite two images using imagemagick's composite command
|
| 130 |
*
|
| 131 |
* @param $tail
|
| 132 |
* path to a smaller image that will be pasted on top of the donkey
|
| 133 |
* @param $donkey
|
| 134 |
* path to a full size image that is the base image
|
| 135 |
* @param $outfile
|
| 136 |
* path to the composite of $donkey and $tail
|
| 137 |
* if $donkey == $outfile, $donkey will be replaced with the composite image
|
| 138 |
* @param $dx
|
| 139 |
* x offset of where to place the $tail on the $donkey
|
| 140 |
* @param $dy
|
| 141 |
* y offset of where to place the $tail on the $donkey
|
| 142 |
* @param $options
|
| 143 |
* any composite specific options you wish to pass (man composite(1))
|
| 144 |
*
|
| 145 |
* @return
|
| 146 |
* nothing
|
| 147 |
*/
|
| 148 |
function acidfree_compose_image($tail, $donkey, $outfile, $dx=0, $dy=0, $options='') {
|
| 149 |
if ($outfile == $donkey) {
|
| 150 |
$outfile = _image_manip_mktmpfile('imagemanip', _acidfree_ext_from_file($small));
|
| 151 |
$temp = true;
|
| 152 |
} else {
|
| 153 |
$temp = false;
|
| 154 |
}
|
| 155 |
image_composite($donkey, $tail, $outfile, $dx, $dy);
|
| 156 |
if ($temp) {
|
| 157 |
file_move($outfile, $donkey, FILE_EXISTS_REPLACE);
|
| 158 |
}
|
| 159 |
}
|
| 160 |
|
| 161 |
function _acidfree_ext_from_file($filename) {
|
| 162 |
$parts = pathinfo($filename);
|
| 163 |
return strtolower($parts['extension']);
|
| 164 |
}
|
| 165 |
|
| 166 |
?>
|