[ PHPXref.com ] [ Generated: Sun Jul 20 19:44:37 2008 ] [ PHPImageMage 1.2.1 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/ -> pim.php (source)

   1  <?php
   2  /*
   3  PHPImageMage v1.2.1, image upload and toolkit.
   4  Copyright (c) 2005, Daniel Löfquist <daniel@kingsofcode.net>
   5  All rights reserved.
   6  
   7  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
   8  
   9  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  11  * The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
  12  
  13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14  */
  15  
  16  //Fetch everything sent from the form, upload
  17  //original image and create variables for it all.
  18  $errormessage="";
  19  $imgdir=$_POST['imgdir'];
  20  if(strrchr($imgdir, "/")!="/"&&strlen($imgdir)>0){$imgdir=$imgdir."/";}
  21  $thumbdir=$_POST['thumbdir'];
  22  if(strrchr($thumbdir, "/")!="/"&&strlen($thumbdir)>0){$thumbdir=$thumbdir."/";}
  23  
  24  //Check what kind of images it is (JPG, GIF or PNG)
  25  //and create a corresponding image-object.
  26  if(isset($_FILES['imagefile'])){
  27      switch($_FILES['imagefile']['type']) {
  28          case 'image/jpeg':
  29          case 'image/pjpeg':
  30              $origimg=imagecreatefromjpeg($_FILES['imagefile']['tmp_name']);
  31              break;
  32          case 'image/png':
  33              $origimg=imagecreatefrompng($_FILES['imagefile']['tmp_name']);
  34              break;
  35          case 'image/gif':
  36              $origimg=imagecreatefromgif($_FILES['imagefile']['tmp_name']);
  37              break;
  38          default:
  39              $errormessage.="Invalid image. Unknown File Format or MIME Type.\n";
  40              $show="error";
  41      }
  42  
  43      $origX=imagesx($origimg);
  44      $origY=imagesy($origimg);
  45      
  46      //Check for resize
  47      if(isset($_POST['do_resize'])&&$_POST['do_resize']=="yes"){
  48          //If resize
  49          if(isset($_POST['resize_X'])&&isset($_POST['resize_Y'])&&
  50              $_POST['resize_X']>0&&$_POST['resize_Y']>0){
  51              $resizeX=$_POST['resize_X'];
  52              $resizeY=$_POST['resize_Y'];
  53          }else{
  54              $resizeX=$origX;
  55              $resizeY=$origY;
  56              $error.="Couldn't perform resizing on image.\n";
  57              $show="error";
  58          }
  59      }else{
  60          $resizeX=$origX;
  61          $resizeY=$origY;
  62      }
  63      $img=imagecreatetruecolor($resizeX, $resizeY);
  64      imagecopyresampled($img, $origimg, 0, 0, 0, 0, $resizeX, $resizeY, $origX, $origY);
  65      
  66      //Check for rotation
  67      if(isset($_POST['do_rotate'])&&$_POST['do_rotate']=="yes"){
  68          $img=imagerotate($img, $_POST['rotate_degrees'], 0);
  69      }
  70      
  71      //Check for overlay
  72      if(isset($_POST['do_overlay'])&&$_POST['do_overlay']=="yes"){
  73          //If overlay with image
  74          if(isset($_POST['overlay_with'])&&$_POST['overlay_with']=="image"){
  75              //Get overlay-image
  76              if(!$overlayimg=imagecreatefrompng($_FILES['overlayfile']['tmp_name'])){
  77                  $errormessage.="Invalid overlay image. Only PNG images allowed.\n";
  78                  $show="error";
  79              }else{
  80                  //Perform overlay
  81                  //If overlay, where to place the overlay (top left,
  82                  //top right, bottom left, bottom right or center)
  83                  $overlayX=imagesx($overlayimg);
  84                  $overlayY=imagesy($overlayimg);
  85                  switch($_POST['overlay_where']){
  86                      case 'tl':
  87                          $overlayposX=0;
  88                          $overlayposY=0;
  89                          break;
  90                      case 'tr':
  91                          $overlayposX=$resizeX-$overlayX;
  92                          $overlayposY=0;
  93                          break;
  94                      case 'bl':
  95                          $overlayposX=0;
  96                          $overlayposY=$resizeY-$overlayY;
  97                          break;
  98                      case 'center':
  99                          $overlayposX=($resizeX-$overlayX)/2;
 100                          $overlayposY=($resizeY-$overlayY)/2;
 101                          break;
 102                      default:
 103                          $overlayposX=$resizeX-$overlayX;
 104                          $overlayposY=$resizeY-$overlayY;
 105                          break;
 106                  }
 107                  
 108                  imagecolortransparent($overlayimg,
 109                                      imagecolorallocate($img, 255, 255, 255));
 110                  imagecopymerge($img, $overlayimg, $overlayposX, $overlayposY,
 111                                  0, 0, $overlayX, $overlayY, 100);
 112              }
 113          //If overlay with text
 114          }else if(isset($_POST['overlay_with'])&&$_POST['overlay_with']=="text"){
 115              //If overlay, where to place the overlay (top left,
 116              //top right, bottom left, bottom right or center)
 117              switch($_POST['overlay_where']){
 118                  case 'tl':
 119                      $overlayposX=2;
 120                      $overlayposY=2;
 121                      break;
 122                  case 'tr':
 123                      $overlayposX=$resizeX-(strlen($_POST['overlay_text'])*8);
 124                      $overlayposY=2;
 125                      break;
 126                  case 'bl':
 127                      $overlayposX=3;
 128                      $overlayposY=$resizeY-15;
 129                      break;
 130                  case 'center':
 131                      $overlayposX=($resizeX-(strlen($_POST['overlay_text'])*8))/2;
 132                      $overlayposY=($resizeY-15)/2;
 133                      break;
 134                  default:
 135                      $overlayposX=$resizeX-(strlen($_POST['overlay_text'])*8);
 136                      $overlayposY=$resizeY-15;
 137                      break;
 138              }
 139              switch($_POST['overlay_color']){
 140                  case 'black':
 141                      $overlaycolor=0;
 142                      break;
 143                  case 'white':
 144                      $overlaycolor=imagecolorallocate($img, 255, 255, 255);
 145                      break;
 146                  case 'red':
 147                      $overlaycolor=imagecolorallocate($img, 255, 0, 0);
 148                      break;
 149                  case 'green':
 150                      $overlaycolor=imagecolorallocate($img, 0, 255, 0);
 151                      break;
 152                  default:
 153                      $overlaycolor=imagecolorallocate($img, 0, 0, 255);
 154                      break;
 155              }
 156              imagestring($img, 3, $overlayposX, $overlayposY, $_POST['overlay_text'], $overlaycolor);
 157          }
 158      }
 159  
 160      //Save image
 161  	function iptc_maketag($rec, $dat, $val){
 162         $len=strlen($val);
 163        if($len<0x8000){
 164            return chr(0x1c).chr($rec).chr($dat).chr($len >> 8).chr($len & 0xff).$val;
 165           }else{
 166           return chr(0x1c).chr($rec).chr($dat).chr(0x80).chr(0x04).
 167                  chr(($len>>24)&0xff).chr(($len>>16)&0xff).
 168                  chr(($len>>8)&0xff).chr(($len)&0xff).$val;
 169           }
 170      }
 171      
 172  	function add_img_caption($image_name, $category, $comment){
 173          $size=getimagesize($image_name, $info);
 174          $iptc_old=iptcparse($info["APP13"]);
 175          //Image category tag
 176          $iptc_old["2#015"][0]=$category;
 177          //Image caption tag
 178          $iptc_old["2#120"][0]=$comment;
 179  
 180          foreach (array_keys($iptc_old) as $s){
 181              // Finds the IPTC numbers
 182              $tag=str_replace("2#", "", $s);
 183              // Creating the string
 184              $iptc_new.=iptc_maketag(2, $tag, $iptc_old[$s][0]);
 185          }
 186          $mode=0;
 187          $content=iptcembed($iptc_new, $image_name, $mode);
 188  
 189          $fp=fopen($image_name, "w");
 190          fwrite($fp, $content);
 191          fclose($fp);
 192      }
 193      
 194      //Check for thumbnail creation
 195      if(isset($_POST['do_thumbnail'])&&$_POST['do_thumbnail']=="yes"){
 196          //If thumbnail creation
 197          if(isset($_POST['thumbnail_X'])&&isset($_POST['thumbnail_Y'])&&
 198              $_POST['thumbnail_X']>0&&$_POST['thumbnail_Y']>0){
 199              $thumbX=$_POST['thumbnail_X'];
 200              $thumbY=$_POST['thumbnail_Y'];
 201              $thumbimg=imagecreatetruecolor($thumbX, $thumbY);
 202              imagecopyresampled($thumbimg, $img, 0, 0, 0, 0, $thumbX, $thumbY, $resizeX, $resizeY);
 203              switch($_FILES['imagefile']['type']) {
 204                  case 'image/jpeg':
 205                  case 'image/pjpeg':
 206                      imagejpeg($thumbimg, $thumbdir.$_FILES['imagefile']['name']);
 207                      if(isset($_POST['do_iptc'])&&$_POST['do_iptc']=="yes"){
 208                          add_img_caption($thumbdir.$_FILES['imagefile']['name'], $_POST['iptc_category']."(THUMBNAIL)", $_POST['iptc_caption']."(THUMBNAIL)");
 209                      }
 210                      break;
 211                  case 'image/png':
 212                      imagepng($thumbimg, $thumbdir.$_FILES['imagefile']['name']);
 213                      break;
 214                  case 'image/gif':
 215                      imagegif($thumbimg, $thumbdir.$_FILES['imagefile']['name']);
 216                      break;
 217                  default:
 218                      $errormessage.="Error when saving thumbnail.\n";
 219                      $show="error";
 220              }
 221          }else{
 222          }
 223      }
 224      
 225      switch($_FILES['imagefile']['type']) {
 226          case 'image/jpeg':
 227          case 'image/pjpeg':
 228              imagejpeg($img, $imgdir.$_FILES['imagefile']['name']);
 229              if(isset($_POST['do_iptc'])&&$_POST['do_iptc']=="yes"){
 230                  add_img_caption($imgdir.$_FILES['imagefile']['name'], $_POST['iptc_category'], $_POST['iptc_caption']);
 231              }
 232              break;
 233          case 'image/png':
 234              imagepng($img, $imgdir.$_FILES['imagefile']['name']);
 235              break;
 236          case 'image/gif':
 237              imagegif($img, $imgdir.$_FILES['imagefile']['name']);
 238              break;
 239          default:
 240              $errormessage.="Error when saving image.\n";
 241              $show="error";
 242      }
 243  
 244  }
 245  
 246  ?>
 247  <html>
 248  <head>
 249  <title>::: PHPImageMage ::: All done</title>
 250  <link rel="stylesheet" type="text/css" href="pim.css"/>
 251  </head>
 252  <body>
 253  <h2>PHPImageMage v1.2.1</h2>
 254  <table class="mainframe" width="240">
 255  <tr><td>
 256  Upload and magic done!<br/>
 257  <?php
 258  echo("Image: <a href=\"".$imgdir.$_FILES['imagefile']['name']."\">".$imgdir.$_FILES['imagefile']['name']."</a><br/>");
 259  if($_POST['do_thumbnail']=="yes"){
 260      echo("Thumbnail: <a href=\"".$thumbdir.$_FILES['imagefile']['name']."\">".$thumbdir.$_FILES['imagefile']['name']."</a><br/>");
 261  }
 262  if(strlen($errormessage)>0){
 263      echo("<br/>".nl2br($errormessage)."<br/>");
 264  }
 265  ?>
 266  <br/>
 267  <a href="pim.html">Back to form</a>
 268  </td></tr>
 269  </table>
 270  </body>
 271  </html>


[ Powered by PHPXref - Served by Debian GNU/Linux ]