Update of /cvsroot/php-blog/serendipity/include
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27464
Modified Files:
functions_images.inc.php
Log Message:
* refactor image rotating to not use duplicate code
* use 4-space indenting
Index: functions_images.inc.php
===================================================================
RCS file: /cvsroot/php-blog/serendipity/include/functions_images.inc.php,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- functions_images.inc.php 10 Dec 2004 16:56:05 -0000 1.14
+++ functions_images.inc.php 10 Dec 2004 17:38:56 -0000 1.15
@@ -264,32 +264,34 @@
* Rotate an Image
**/
function serendipity_rotateImg($id, $degrees) {
- global $serendipity;
+ global $serendipity;
- $file = serendipity_fetchImageFromDatabase($id);
+ $file = serendipity_fetchImageFromDatabase($id);
- $admin = '';
- if ($serendipity['serendipityUserlevel'] < USERLEVEL_CHIEF && $file['authorid'] != '0' && $file['authorid'] != $serendipity['authorid']) {
- // A non-admin user may not delete private files from other users.
- return;
- }
+ $admin = '';
+ if ($serendipity['serendipityUserlevel'] < USERLEVEL_CHIEF && $file['authorid'] != '0' && $file['authorid'] != $serendipity['authorid']) {
+ // A non-admin user may not delete private files from other users.
+ return;
+ }
- $infile = $outfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file['path'] . $file['name'] . '.' . $file['extension'];
- $infileThumb = $outfileThumb = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file['path'] . $file['name'] . '.' . $file['thumbnail_name'] . '.'. $file['extension'];
+ $infile = $outfile = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file['path'] . $file['name'] . '.' . $file['extension'];
+ $infileThumb = $outfileThumb = $serendipity['serendipityPath'] . $serendipity['uploadPath'] . $file['path'] . $file['name'] . '.' . $file['thumbnail_name'] . '.'. $file['extension'];
- if ($serendipity['magick'] !== true) {
- serendipity_rotate_image_gd($infile, $outfile, $degrees);
- serendipity_rotate_image_gd($infileThumb, $outfileThumb, $degrees);
- } else {
- $cmd = $serendipity['convert'] . ' -rotate ' . $degrees . ' ' . $infile . ' ' . $outfile;
- $res = `$cmd`;
- $cmdThumb = $serendipity['convert'] . ' -rotate ' . $degrees . ' ' . $infileThumb . ' ' . $outfileThumb;
- $resThumb = `$cmdThumb`;
- }
- $fdim = @getimagesize($outfile);
+ if ($serendipity['magick'] !== true) {
+ serendipity_rotate_image_gd($infile, $outfile, $degrees);
+ serendipity_rotate_image_gd($infileThumb, $outfileThumb, $degrees);
+ } else {
+ $cmd = $serendipity['convert'] . ' -rotate ' . $degrees . ' ' . $infile . ' ' . $outfile;
+ $res = `$cmd`;
+ $cmdThumb = $serendipity['convert'] . ' -rotate ' . $degrees . ' ' . $infileThumb . ' ' . $outfileThumb;
+ $resThumb = `$cmdThumb`;
+ }
- serendipity_updateImageInDatabase(array('dimensions_width' => $fdim[0], 'dimensions_height' => $fdim[1]), $id);
- return;
+ $fdim = @getimagesize($outfile);
+
+ serendipity_updateImageInDatabase(array('dimensions_width' => $fdim[0], 'dimensions_height' => $fdim[1]), $id);
+
+ return;
}
@@ -628,29 +630,29 @@
return $i;
}
-function serendipity_rotate_image_gd($infilename, $outfilename, $degrees)
-{
+function serendipity_functions_gd($infilename) {
if (!function_exists('imagecopyresampled')) {
return false;
}
- $inf = pathinfo(strtolower($infilename));
+ $func = array();
+ $inf = pathinfo(strtolower($infilename));
switch ($inf['extension']) {
case 'gif':
- $loadfn = 'imagecreatefromgif';
- $savefn = 'imagegif';
+ $func['load'] = 'imagecreatefromgif';
+ $func['save'] = 'imagegif';
break;
case 'jpeg':
case 'jpg':
case 'jfif':
- $loadfn = 'imagecreatefromjpeg';
- $savefn = 'imagejpeg';
+ $func['load'] = 'imagecreatefromjpeg';
+ $func['save'] = 'imagejpeg';
break;
case 'png':
- $loadfn = 'imagecreatefrompng';
- $savefn = 'imagepng';
+ $func['load'] = 'imagecreatefrompng';
+ $func['save'] = 'imagepng';
break;
default:
@@ -658,23 +660,33 @@
}
/* If our loader does not exist, we are doomed */
- if (!function_exists($loadfn)) {
+ if (!function_exists($func['load'])) {
return false;
}
/* If the save function does not exist (i.e. read-only GIF), we want to output it as PNG */
- if (!function_exists($savefn)) {
+ if (!function_exists($func['save'])) {
if (function_exists('imagepng')) {
- $savefn = 'imagepng';
+ $func['save'] = 'imagepng';
} else {
return false;
}
}
- $in = $loadfn($infilename);
+ return $func;
+}
+
+function serendipity_rotate_image_gd($infilename, $outfilename, $degrees)
+{
+ $func = serendipity_functions_gd($infilename);
+ if (!is_array($func)) {
+ return false;
+ }
+
+ $in = $func['load']($infilename);
$out = imagerotate($in, $degrees, 0);
- $savefn($out, $outfilename);
+ $func['save']($out, $outfilename);
$newwidth = imagesx($out);
$newheight = imagesy($out);
@@ -688,48 +700,12 @@
function serendipity_resize_image_gd($infilename, $outfilename, $newwidth, $newheight=null)
{
- if (!function_exists('imagecopyresampled')) {
- return false;
- }
-
- $inf = pathinfo(strtolower($infilename));
- switch ($inf['extension']) {
- case 'gif':
- $loadfn = 'imagecreatefromgif';
- $savefn = 'imagegif';
- break;
-
- case 'jpeg':
- case 'jpg':
- case 'jfif':
- $loadfn = 'imagecreatefromjpeg';
- $savefn = 'imagejpeg';
- break;
-
- case 'png':
- $loadfn = 'imagecreatefrompng';
- $savefn = 'imagepng';
- break;
-
- default:
- return false;
- }
-
- /* If our loader does not exist, we are doomed */
- if (!function_exists($loadfn)) {
+ $func = serendipity_functions_gd($infilename);
+ if (!is_array($func)) {
return false;
}
- /* If the save function does not exist (i.e. read-only GIF), we want to output it as PNG */
- if (!function_exists($savefn)) {
- if (function_exists('imagepng')) {
- $savefn = 'imagepng';
- } else {
- return false;
- }
- }
-
- $in = $loadfn($infilename);
+ $in = $func['load']($infilename);
$width = imagesx($in);
$height = imagesy($in);
@@ -758,7 +734,7 @@
$out = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($out, $in, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
- $savefn($out, $outfilename);
+ $func['save']($out, $outfilename);
$out = null;
$in = null;
@@ -992,8 +968,8 @@
// File is PDF -> Thumb is PNG
if ($file['mime'] == 'application/pdf') {
- $file['imgsrc'] .= '.png';
- $file['displaymime'] = 'image/png';
+ $file['imgsrc'] .= '.png';
+ $file['displaymime'] = 'image/png';
}
return (0 === strpos(strtolower($file['displaymime']), 'image/'));
|