aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGreg Froese <greg.froese@gmail.com>2009-05-11 05:44:02 +0000
committerGreg Froese <greg.froese@gmail.com>2009-05-11 05:44:02 +0000
commitd9964ef61832a507ed78a0e4bf4208700cb16f9f (patch)
tree627da4d6a266727fcc816bb3f0429345cfb2b931
parent076994be737bc012ea0017c2a5fa2af842911cce (diff)
downloadelgg-d9964ef61832a507ed78a0e4bf4208700cb16f9f.tar.gz
elgg-d9964ef61832a507ed78a0e4bf4208700cb16f9f.tar.bz2
imagemagick support for creating thumbnails and watermarking
-rw-r--r--actions/resize.php109
-rw-r--r--actions/upload.php65
2 files changed, 170 insertions, 4 deletions
diff --git a/actions/resize.php b/actions/resize.php
new file mode 100644
index 000000000..8a8a5bb34
--- /dev/null
+++ b/actions/resize.php
@@ -0,0 +1,109 @@
+<?php
+
+/*
+ * Gets the jpeg contents of the resized version of an already uploaded image
+ * (Returns false if the uploaded file was not an image)
+ *
+ * @param string $input_name The name of the file input field on the submission form
+ * @param string $prefix The text to prefix to the existing filename
+ * @param int $maxwidth The maximum width of the resized image
+ * @param int $maxheight The maximum height of the resized image
+ * @param true|false $square If set to true, will take the smallest of maxwidth and maxheight and use it to set the dimensions on all size; the image will be cropped.
+ * @return false|mixed The contents of the resized image, or false on failure
+ */
+ function tp_resize($input_name, $prefix, $maxwidth, $maxheight, $square = false, $x1 = 0, $y1 = 0, $x2 = 0, $y2 = 0) {
+ $params = array(
+ "input_name"=>$input_name,
+ "output_name"=>$output_name,
+ "maxwidth"=>$maxwidth,
+ "maxheight"=>$maxheight,
+ "square"=>$square,
+ "x1"=>$x1,
+ "y1"=>$y1,
+ "x2"=>$x2,
+ "y2"=>$y2);
+
+ $path = pathinfo($input_name);
+ $output_name = $path["dirname"] . "/$prefix" . $path["filename"] . "." . $path["extension"];
+
+ // Get the size information from the image
+ if ($imgsizearray = getimagesize($input_name)) {
+
+ // Get width and height
+ $width = $imgsizearray[0];
+ $height = $imgsizearray[1];
+ $newwidth = $width;
+ $newheight = $height;
+
+ // Square the image dimensions if we're wanting a square image
+ if ($square) {
+ if ($width < $height) {
+ $height = $width;
+ } else {
+ $width = $height;
+ }
+
+ $newwidth = $width;
+ $newheight = $height;
+
+ }
+
+ if ($width > $maxwidth) {
+ $newheight = floor($height * ($maxwidth / $width));
+ $newwidth = $maxwidth;
+ }
+ if ($newheight > $maxheight) {
+ $newwidth = floor($newwidth * ($maxheight / $newheight));
+ $newheight = $maxheight;
+ }
+
+ $accepted_formats = array(
+ 'image/jpeg' => 'jpeg',
+ 'image/png' => 'png',
+ 'image/gif' => 'gif'
+ );
+ // If it's a file we can manipulate ...
+ if (array_key_exists($imgsizearray['mime'],$accepted_formats)) {
+
+// $function = "imagecreatefrom" . $accepted_formats[$imgsizearray['mime']];
+// $newimage = imagecreatetruecolor($newwidth,$newheight);
+
+// if (is_callable($function) && $oldimage = $function($input_name)) {
+ // Crop the image if we need a square
+ if ($square) {
+ if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
+ $widthoffset = floor(($imgsizearray[0] - $width) / 2);
+ $heightoffset = floor(($imgsizearray[1] - $height) / 2);
+ } else {
+ $widthoffset = $x1;
+ $heightoffset = $y1;
+ $width = ($x2 - $x1);
+ $height = $width;
+ }
+ } else {
+ if ($x1 == 0 && $y1 == 0 && $x2 == 0 && $y2 ==0) {
+ $widthoffset = 0;
+ $heightoffset = 0;
+ } else {
+ $widthoffset = $x1;
+ $heightoffset = $y1;
+ $width = ($x2 - $x1);
+ $height = ($y2 - $y1);
+ }
+ }//else {
+ // Resize and return the image contents!
+ if ($square) {
+ $newheight = $maxheight;
+ $newwidth = $maxwidth;
+ }
+ $command = "convert $input_name -resize ".$newwidth."x".$newheight."^ -gravity center -extent ".$newwidth."x".$newheight." $output_name";
+ system($command);
+ return $output_name;
+
+ }
+ }
+
+ return false;
+ }
+
+?>
diff --git a/actions/upload.php b/actions/upload.php
index ba9d005c8..89e66ca31 100644
--- a/actions/upload.php
+++ b/actions/upload.php
@@ -6,6 +6,7 @@
*/
global $CONFIG;
+ require_once(dirname(__FILE__)."/resize.php");
// Get common variables
$access_id = (int) get_input("access_id");
@@ -61,18 +62,26 @@
//TODO: This code needs a complete rewrite - hardcoded to ~2.5 MB
if (filesize($file->getFilenameOnFilestore())<= 2500000) {
try {
- $thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),600,600, false);
+ //$thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),600,600, false);
} catch (Exception $e) { $thumblarge = false; }
try {
- $thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),153,153, true);
+ //$thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),153,153, true);
} catch (Exception $e) { $thumbsmall = false; }
try {
- $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),60,60, true);
+ //$thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),60,60, true);
} catch (Exception $e) { $thumbnail = false; }
}
+ // gfroese: i'm just faking this out so the proper records are created for the thumbnails
+ // I don't understand what $thumb->write does but it seems to actually create the file so
+ // I had to move the imagemagick call beneath this. I'm sure someone else can make this
+ // much better.
+ $thumbnail = true;
+ $thumblarge = true;
+ $thumbsmall = true;
+
if ($thumbnail) {
$thumb = new ElggFile();
$thumb->setMimeType($mime);
@@ -111,6 +120,54 @@
}
$thumb->close();
}
+
+ //gfroese: build the actual thumbnails now
+ $album = get_entity($container_guid);
+ $user = get_user_entity_as_row($album->owner_guid);
+ $username = $user->username;
+
+ try {
+ $thumblarge = tp_resize($file->getFilenameOnFilestore(), "largethumb", 600, 600, false);
+ } catch (Exception $e) { $thumblarge = false; }
+ try {
+ $thumbsmall = tp_resize($file->getFilenameOnFilestore(), "smallthumb", 153, 153, false);
+ } catch (Exception $e) { $thumbsmall = false; }
+ try {
+ $thumbnail = tp_resize($file->getFilenameOnFilestore(), "thumb", 60, 60, true);
+ } catch (Exception $e) { $thumbnail = false; }
+
+
+ $watermark = true; //gfroese - this should come from the plugin settings
+ $watermark_text = $username . " - shutterpeg.com";
+ if( $watermark ) { //get this value from the plugin settings
+ if( $thumblarge ) {
+ $ext = ".png";
+ $user_stamp_base = dirname(__FILE__) . "/" . $username . "_stamp";
+ if( !file_exists( $user_stamp_base . $ext )) { //create the watermark if it doesn't exist
+ $commands = array();
+ $commands[] = 'convert -size 300x50 xc:grey30 -pointsize 20 -gravity center -draw "fill grey70 text 0,0 \''. $watermark_text . '\'" '. $user_stamp_base . '_fgnd' . $ext;
+ $commands[] = 'convert -size 300x50 xc:black -pointsize 20 -gravity center -draw "fill white text 1,1 \''. $watermark_text . '\' text 0,0 \''. $watermark_text . '\' fill black text -1,-1 \''. $watermark_text . '\'" +matte ' . $user_stamp_base . '_mask' . $ext;
+ $commands[] = 'composite -compose CopyOpacity ' . $user_stamp_base . "_mask" . $ext . ' ' . $user_stamp_base . '_fgnd' . $ext . ' ' . $user_stamp_base . $ext;
+ $commands[] = 'mogrify -trim +repage ' . $user_stamp_base . $ext;
+ $commands[] = 'rm ' . $user_stamp_base . '_mask' . $ext;
+ $commands[] = 'rm ' . $user_stamp_fgnd . '_mask' . $ext;
+
+ foreach( $commands as $command ) {
+ file_put_contents("/home/gfroese/debug.txt", $command . "\n", FILE_APPEND);
+ exec( $command );
+ }
+ }
+ //apply the watermark
+ $commands = array();
+ $commands[] = 'composite -gravity south -geometry +0+10 ' . $user_stamp_base . $ext . ' ' . $thumblarge . ' ' . $thumblarge . '_watermarked';
+ $commands[] = "mv $thumblarge" . "_watermarked $thumblarge";
+ foreach( $commands as $command ) {
+ file_put_contents("/home/gfroese/debug.txt", $command . "\n", FILE_APPEND);
+ exec( $command );
+ }
+ }
+ }
+
} else { //file exceeds file size limit, so delete it
$file->delete();
array_push($not_uploaded, $name);
@@ -149,4 +206,4 @@
forward(get_input('forward_url', $_SERVER['HTTP_REFERER'])); //upload failed, so forward to previous page
}
-?> \ No newline at end of file
+?>