[ Index ]

PHP Cross Reference of phpwcms V1.4.3 _r380 (23.11.09)

title

Body

[close]

/ -> image_resized.php (source)

   1  <?php
   2  /*************************************************************************************
   3     Copyright notice
   4     
   5     (c) 2002-2009 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
   6   
   7     This script is part of PHPWCMS. The PHPWCMS web content management system is
   8     free software; you can redistribute it and/or modify it under the terms of
   9     the GNU General Public License as published by the Free Software Foundation;
  10     either version 2 of the License, or (at your option) any later version.
  11    
  12     The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
  13     A copy is found in the textfile GPL.txt and important notices to the license 
  14     from the author is found in LICENSE.txt distributed with these scripts.
  15    
  16     This script is distributed in the hope that it will be useful, but WITHOUT ANY 
  17     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  18     PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  19   
  20     This copyright notice MUST APPEAR in all copies of the script!
  21  *************************************************************************************/
  22  
  23  // <img src="image_resized.php?format=jpg&w=100&h=200&q=85&imgfile=test.jpg" alt="" border="0">
  24  
  25  $img_target    = (isset($_GET['format'])) ? strtolower(trim($_GET['format'])) : 'jpg';
  26  $img_file    = (isset($_GET['imgfile'])) ? trim($_GET['imgfile']) : 'img/leer.gif';
  27  $img_width    = (isset($_GET['w'])) ? intval($_GET['w']) : 0;
  28  $img_height    = (isset($_GET['h'])) ? intval($_GET['h']) : 0;
  29  $img_quality= (isset($_GET['q']) && intval($_GET['q']) <= 100 && intval($_GET['q'])) ? intval($_GET['q']) : 75;
  30  
  31  $img_file    = str_replace(array('http://', 'https://'), '', $img_file);
  32  
  33  switch($img_target) {
  34  
  35      case 'jpg':        $img_mimetype = 'image/jpeg';
  36                      break;
  37      
  38      case 'jpeg':    $img_mimetype = 'image/jpeg';
  39                      $img_target = 'jpg';
  40                      break;
  41      
  42      case 'png':        $img_mimetype = 'image/png';
  43                      break;
  44      
  45      case 'gif':        if(function_exists('imagegif')) {
  46                          $img_mimetype = 'image/gif';
  47                      } else {
  48                          $img_target   = 'png';
  49                          $img_mimetype = 'image/png';
  50                      }
  51                      break;
  52      
  53      default:        $img_mimetype    = 'image/jpeg';
  54                      $img_target        = 'jpg';
  55  
  56  }
  57  
  58  if(is_file($img_file) && $img_info = getimagesize($img_file)) {
  59      
  60      if(!$img_width || $img_width >= $img_info[0]) {
  61          $percent_width = 1;
  62      } else {
  63          $percent_width = $img_width / $img_info[0];
  64      }
  65      
  66      if(!$img_height || $img_height >= $img_info[1]) {
  67          $percent_height = 1;
  68      } else {
  69          $percent_height = $img_height / $img_info[1];
  70      }
  71      
  72      if($percent_height < $percent_width) {
  73          $percent = $percent_height;
  74      } elseif($percent_height > $percent_width) {
  75          $percent = $percent_width;
  76      } else {
  77          $percent = $percent_width;
  78      }
  79  
  80      
  81      $img_width    = ($img_info[0] * $percent);
  82      $img_height    = ($img_info[1] * $percent);
  83      
  84      
  85      switch($img_target) {
  86  
  87          case 'jpg':        $new_img = imagecreatetruecolor($img_width, $img_height);
  88                          break;
  89      
  90          case 'png':        $new_img = imagecreatetruecolor($img_width, $img_height);
  91                          break;
  92      
  93          case 'gif':        $new_img = imagecreate($img_width, $img_height);
  94                          break;
  95  
  96      }
  97      
  98      switch($img_info[2]) {
  99      
 100          case 1:    // GIF
 101                  $img_source = imagecreatefromgif($img_file);
 102                  break;
 103          
 104          case 2:    // JPG
 105                  $img_source = imagecreatefromjpeg($img_file);
 106                  break;
 107          
 108          case 3:    // PNG
 109                  $img_source = imagecreatefrompng($img_file);
 110                  break;
 111      
 112      }
 113      
 114      imagecopyresized($new_img, $img_source, 0, 0, 0, 0, $img_width, $img_height, $img_info[0], $img_info[1]);
 115      
 116      header('Content-type: '.$img_mimetype);
 117      
 118      switch($img_target) {
 119  
 120          case 'jpg':        imagejpeg($new_img, '', $img_quality);
 121                          break;
 122      
 123          case 'png':        imagepng($new_img, '', 9);
 124                          break;
 125      
 126          case 'gif':        imagegif($new_img);
 127                          break;
 128  
 129      }
 130      
 131      imagedestroy($new_img);
 132      imagedestroy($img_source);
 133      
 134  } else {
 135  
 136      // error / no image
 137      header ('Content-type: image/png');
 138      $new_img = imagecreatetruecolor(75, 20);
 139      $text_color = imagecolorallocate($new_img, 255, 255, 255);
 140      imagestring($new_img, 1, 5, 5,  "Image Error", $text_color);
 141      imagepng($new_img, '', 9);
 142      imagedestroy($new_img);
 143  
 144  
 145  }
 146  
 147  ?>


Generated: Wed Dec 30 05:55:15 2009 Cross-referenced by PHPXref 0.7