]> git.mjollnir.org Git - moodle.git/commitdiff
"MDL-18830, add watermark to flickr image"
authordongsheng <dongsheng>
Sun, 5 Jul 2009 14:15:18 +0000 (14:15 +0000)
committerdongsheng <dongsheng>
Sun, 5 Jul 2009 14:15:18 +0000 (14:15 +0000)
repository/flickr_public/image.php [new file with mode: 0755]
repository/flickr_public/repository.class.php

diff --git a/repository/flickr_public/image.php b/repository/flickr_public/image.php
new file mode 100755 (executable)
index 0000000..bb4d558
--- /dev/null
@@ -0,0 +1,149 @@
+<?php
+class moodle_image {
+       private $imagepath;
+       private $info;
+       private $width;
+       private $height;
+       private $image;
+       private $backup;
+
+       function __construct($img) {
+        if(!function_exists('imagecreatefrompng')
+            and !function_exists('imagecreatefromjpeg')) {
+            throw new moodle_exception('gdnotexist');
+        }
+        if(!file_exists($img) or !is_readable($img)) {
+            throw new moodle_exception('invalidfile');
+        }
+               
+               $this->imagepath = $img;
+        unset($img);
+               $this->info = getimagesize($this->imagepath);
+
+               switch($this->info['mime']) {
+        case 'image/jpeg':
+            $this->image = imagecreatefromjpeg($this->imagepath);
+            break;
+        case 'image/png':
+            $this->image = imagecreatefrompng($this->imagepath);
+            break;
+        case 'image/gif': 
+            $this->image = imagecreatefromgif($this->imagepath); 
+            break;
+        default:
+            break;
+               }
+        if (empty($this->image)) {
+            throw new moodle_exception('invalidimage');
+        }
+               $this->width  = imagesx($this->image);
+               $this->height = imagesy($this->image);
+       }
+
+       function destroy() {
+                imagedestroy($this->image);
+                imagedestroy($this->backup);
+         return true;
+       }
+
+       function undo() {
+               $this->image = $this->backup;
+               return $this;
+       }
+
+    function watermark($text='', $pos=array(), $options=array()) {
+        global $CFG;
+        $text = iconv('ISO-8859-8', 'UTF-8', $text);
+        if (empty($options['fontsize'])) {
+            if (!empty($options['ttf'])) {
+                $options['fontsize'] = 12;
+            } else {
+                $options['fontsize'] = 1;
+            }
+        }
+
+        if (empty($options['font'])) {
+            $options['font'] = $CFG->libdir . '/default.ttf';
+        }
+        if (empty($options['angle'])) {
+            $options['angle'] = 0;
+        }
+        $clr = imagecolorallocate($this->image, 255, 255, 255);
+        if (!empty($options['ttf'])) {
+            imagettftext($this->image,
+                $options['fontsize'],        // font size
+                $options['angle'],
+                $pos[0],
+                $pos[1]+$options['fontsize'],
+                $clr,
+                $options['font'],
+                $text);
+        } else {
+            imagestring($this->image, $options['fontsize'], $pos[0], $pos[1], $text, $clr);
+        }
+        return $this;
+    }
+
+       function rotate($angle=0, $bgcolor=0) {
+               $this->image = imagerotate($this->image, $angle, $bgcolor);
+               return $this;
+       }
+       
+       function resize($w, $h, $use_resize = true) {
+        if(empty($h) && !empty($w)) {
+            $h = $this->height * ($w/$this->width);
+        }
+        if(!empty($h) && empty($w)) {
+            $w = $this->width  * ($h/$this->height);
+        }
+        $new_img = imagecreatetruecolor($w, $h);
+        imagealphablending($new_img, false);
+        imagecopyresampled($new_img /* dst */, $this->image /* src */, 0, 0, 0, 0, $w, $h, $this->width, $this->height);
+        $this->image = $new_img;
+               return $this;
+       }
+
+       function saveas($imagepath='') {
+        if (empty($imagepath)) {
+            $imagepath = $this->imagepath;
+        }
+               switch($this->info['mime']) {
+        case 'image/jpeg':
+            return imagejpeg($this->image, $imagepath);
+            break;
+        case 'image/png':
+            return imagepng($this->image, $imagepath);
+            break;
+        case 'image/gif': 
+            return imagegif($this->image, $imagepath);
+            break;
+        default:
+            break;
+               }
+        if(!$this->destroy()) {
+            return false;
+        } else {
+            return $this;
+        }
+       }
+
+       function display() {
+               header('Content-type: '.$this->info['mime']);
+               switch($this->info['mime']) {
+        case 'image/png':
+            imagepng($this->image);
+            break;
+        case 'image/jpeg':
+            imagejpeg($this->image);
+            break;
+        case 'image/gif':
+            imagegif($this->image);
+            break;
+        default:
+            break;
+               }
+               $this->destroy();
+               return $this;
+       }
+}
+
index 468535e0c334e5e251497b388a634d1bde7653ee..4ed6414eacb3e6a75ac9e021813d6a3fd7f3fa81 100644 (file)
@@ -11,6 +11,7 @@
  */
 
 require_once($CFG->libdir.'/flickrlib.php');
+require_once(dirname(__FILE__) . '/image.php');
 
 /**
  *
@@ -333,7 +334,7 @@ class repository_flickr_public extends repository {
                 $format = '.'.$format;
                 if (substr($p['title'], strlen($p['title'])-strlen($format)) != $format) {
                     // append author id
-                    $p['title'] .= '-'.$p['owner'];
+                    // $p['title'] .= '-'.$p['owner'];
                     // append file extension
                     $p['title'] .= $format; 
                 }
@@ -384,7 +385,8 @@ class repository_flickr_public extends repository {
 
         $watermark = get_config('flickr_public', 'watermark');
         if (!empty($watermark)) {
-            // process watermark
+            $img = new moodle_image($path);
+            $img->watermark($url, array(10,10), array('ttf'=>true, 'fontsize'=>9))->saveas($path);
         }
         return $path;
     }