<?php
/*
class_modpic
* Modifies an image to a special size (with watermarks, too!)
* author: Gabriel Wilkes <willi292@web.de>
* homepage: http://gabriel-wilkes.de
* first created: 2007-12-10
* lastmodify: 2007-12-12

This script is licensed under the GPL v3. The whole text can be read under http://www.gnu.org/licenses/gpl.html.

I hope you like this script. When there are any problems or you have helpful suggestions, just write an email to ich@gabriel-wilkes.de

You may distribute and modify this script as you like. But I would appreciate it, when my name, my email-address and my homepage are mentioned.

How to use it?
--------------------

  Well, at first you have to initialize a new picture, which should be modified:
$pic = new Modpic($picurl, $newsize, $forcesize);
  The 1st parameter ist the URL to the jpg-picture, the seconds sets the size, on which the pic has to be resized and the 3rd sets, whether this size has to be "strict" or the image shrinks proportional  

$pic->newwatermark($string, $fontfile, $fontsize, $position, $color, $transperancy);
  This method sets a new text onto the image. The parameters are: 
    1st - the text, which should be written
    2nd - the fontfile (typically .ttf) which has to be used for write the text on the image [standard: "arial.ttf"]
    3rd - the fontsize [standard: 14]
    4th - the position on the image with one of the following strings: topleft, topcenter, topright, centerleft, center, centerright, bottomleft, bottomcenter or bottomright [standard: "topleft"]
    5th - the color (either one of the 16 color codes or a hexadecimalcode, which starts with a '#' and then has 3 or 6 characters) [standard: "black"]
    6th - the transperancy of the text, from 0 (fully visible) to 120 (invisible) [standard: 0]
  Only the text is required, when the others are not given, the standard value is used
  
$pic->outputimg($url);
  Shows the picture in the browser, when the parameter $url is set, the pic won't be displayed but saved in $url


Examples
------------
  
So, for example the following displays the image "test.jpg" in 200x200px with a blue, non-transparent text in the middle "Hello World!", using "arial.ttf", which is in the same directory, and a fontsize of 16

$pic = new Modpic("test.jpg", 200, 1);
$pic->newwatermark("Hello World!", "arial.ttf", 16, 'center', 'blue', 0);
$pic->outputimg();

 
When a whole folder of pictures has to be altered, use the function glob and change every image seperated. Then you can do something like this, for creating thumbnails with a size of 100px:

foreach (glob("*.jpg") as $filename) {
    $pic = new Modpic($filename, 100, 1);
    $pic->outputimg('thumbs/'.$filename);
}



Have fun!

*/



class Modpic {
  
/* at first, create the properties */
  
private $newpic;
  private 
$width;
  private 
$height;
  
  function 
__construct($url$maxsize$forcesize 0){

    
/* get the size of the image */
    
if(!$org_pic = @imagecreatefromjpeg($url)) die("Sorry, the image is not a valid JPEG-Image.");
    
$dimensions getimagesize($url);
    
$org_width $dimensions[0];
    
$org_height $dimensions[1];
    
    if(
$forcesize){ 
    
/* when $forcesize is true, the image gets squarish - but the clipping has to be so, that no black bar appears */

      
$this->newpic imagecreatetruecolor($maxsize$maxsize);
      if(
$org_width $org_height){
        
$this->width $maxsize;
        
$this->height = (($maxsize $org_width) * $org_height);
        if(
$this->height $maxsize) {
          
$pos_left = -((($maxsize $org_height) * $org_width) / $maxsize 2);
          
$this->width = (/ ($maxsize / ($maxsize $this->height))) * $this->width $this->width;
          
$this->height $maxsize;
        }
        
$pos_top 0;
      } else {
        
$this->height $maxsize;
        
$this->width = ($maxsize $org_height) * $org_width;
        if(
$this->width $maxsize) {
          
$pos_top = -((($maxsize $org_width) * $org_height) / $maxsize 2);
          
$this->height = (/ ($maxsize / ($maxsize $this->width))) * $this->height $this->height;
          
$this->width $maxsize;
        }
        
$pos_left 0;
      }
      
imagecopyresampled($this->newpic$org_pic$pos_left$pos_top00$this->width$this->height$org_width$org_height);
      
$this->width $maxsize;
      
$this->height $maxsize;
      

    } else {
    
/* otherwise the image is scaled down proportional */

          
if($org_width $org_height){
              
$this->width $maxsize;
              
$this->height = ($maxsize/$org_width)*$org_height;
          } else {
                 
$this->height $maxsize;
                 
$this->width = ($maxsize/$org_height)*$org_width;
          }  
          
$this->newpic ImageCreateTrueColor($this->width$this->height);
          if(
$org_width $this->width){
              
ImageCopyresampled($this->newpic$org_pic0000$this->width$this->height$org_width$org_height);
          } else {
              
$this->newpic $org_pic// when the original picture is bigger than the new one should be, the old is used
              
$this->width $org_width;
              
$this->height $org_height;
          }
      }
      
imagedestroy($org_pic);
      

  }

  function 
newwatermark($text$fontfile 'arial.ttf'$fontsize 14$pos 'topleft'$color 'black'$transperancy 0){
  
/* creates a new text on the picture, a "watermark" */
          
$colors = array('black' => '#000''maroon' => '#800000''green' => '#080''olive' => '#808000''navy' => '#000080''purple' => '#800080''teal' => '#008080''silver' => '#C0C0C0''grey' => '#808080''red' => '#F00''lime' => '#0F0''yellow' => '#FF0''blue' => '#00F''fuchsia' => '#F0F''aqua' => '#0FF''white' => '#FFF');
          if(
array_key_exists($color$colors)) $color $colors[$color]; // when $color is a colorname and not a hexadecimal value, the variable first becomes a hexadecimal value

          
if(strlen($color) == 7){ // when $color has 7 characters (e.g. #0A0A0A) …
            
$red hexdec($color{1}.$color{2});
            
$green hexdec($color{3}.$color{4});
            
$blue hexdec($color{5}.$color{6});
          } else { 
// otherwise make that
            
$red hexdec($color{1}.$color{1});
            
$green hexdec($color{2}.$color{2});
            
$blue hexdec($color{3}.$color{3});
          }
          
          
$color imagecolorallocatealpha($this->newpic$red$green$blue$transperancy); // make a GD-lib-color from the $red, $green, $blue and $transperancy values
            
               
$textsize imagettfbbox($fontsize0$fontfile$text); // get the size of the text
               
$w_width = - ($textsize[0] - $textsize[4]);
               
$w_height = - ($textsize[1] - $textsize[5]);

          switch (
$pos) {
          
/* gets the $x and $y values from the string, where to position the text */
            
case 'center':
              
$x = ($this->width $w_width) / 2;
              
$y = ($this->height $w_height) / 2;
              break;
            case 
'topleft':
              
$x 5;
              
$y = -$w_height 5;
              break;
            case 
'topright' :
              
$x $this->width $w_width 5;
              
$y abs($w_height) + 5;
              break;
            case 
'topcenter':
              
$x = ($this->width $w_width) / 2;
              
$y abs($w_height) + 5;
              break;
            case 
'centerleft':
              
$x 0;
              
$y = ($this->height $w_height) / 2;
              break;
            case 
'centerright':
              
$x $this->width $w_width 5;
              
$y = ($this->height $w_height) / 2;
              break;
            case 
'bottomleft':
              
$x 2;
              
$y $this->height 5;
              break;
            case 
'bottomcenter':
              
$x = ($this->width $w_width) / 2;
              
$y $this->height 5;
              break;
            case 
'bottomright':
              
$x $this->width $w_width 5;
              
$y $this->height 5;
              break;
          }

               
imagettftext($this->newpic$fontsize0$x$y$color$fontfile$text); // at least, put the text on the pic
  
}
  
  function 
outputimg($outputurl NULL){
    if(
$outputurl){ // if there is given a url, save the image
      
imagejpeg($this->newpic$outputurl);
    } else { 
// otherwise display it directly
      
header("Content-Type: image/jpeg"); // header is important, otherwise there is only an alphabet soup ;-)
      
imagejpeg($this->newpic""100);
    }
    
imagedestroy($this->newpic); // destroy the image, to clear the memory
  
}
}

?>