[ Index ]

PHP Cross Reference of phpwcms V1.5.0 _r431 (28.01.12)

title

Body

[close]

/ -> image_resized.php (source)

   1  <?php
   2  /*************************************************************************************
   3     Copyright notice
   4     
   5     (c) 2002-2012 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 'png':        $img_mimetype    = 'image/png';
  36                      $img_target        = 'jpg';
  37                      break;
  38      
  39      case 'gif':        if(function_exists('imagegif')) {
  40                          $img_mimetype = 'image/gif';
  41                          $img_target   = 'gif';
  42                      } else {
  43                          $img_target   = 'png';
  44                          $img_mimetype = 'image/png';
  45                      }
  46                      break;
  47      
  48      case 'jpeg':
  49      case 'jpg':
  50      default:        $img_mimetype    = 'image/jpeg';
  51                      $img_target        = 'jpg';
  52  
  53  }
  54  
  55  if(is_file($img_file) && $img_info = getimagesize($img_file)) {
  56      
  57      if(!$img_width || $img_width >= $img_info[0]) {
  58          $percent_width = 1;
  59      } else {
  60          $percent_width = $img_width / $img_info[0];
  61      }
  62      
  63      if(!$img_height || $img_height >= $img_info[1]) {
  64          $percent_height = 1;
  65      } else {
  66          $percent_height = $img_height / $img_info[1];
  67      }
  68      
  69      if($percent_height < $percent_width) {
  70          $percent = $percent_height;
  71      } elseif($percent_height > $percent_width) {
  72          $percent = $percent_width;
  73      } else {
  74          $percent = $percent_width;
  75      }
  76  
  77      
  78      $img_width    = ($img_info[0] * $percent);
  79      $img_height    = ($img_info[1] * $percent);
  80      
  81      
  82      switch($img_target) {
  83  
  84          case 'jpg':        $new_img = imagecreatetruecolor($img_width, $img_height);
  85                          break;
  86      
  87          case 'png':        $new_img = imagecreatetruecolor($img_width, $img_height);
  88                          break;
  89      
  90          case 'gif':        $new_img = imagecreate($img_width, $img_height);
  91                          break;
  92  
  93      }
  94      
  95      switch($img_info[2]) {
  96      
  97          case 1:    // GIF
  98                  $img_source = imagecreatefromgif($img_file);
  99                  break;
 100          
 101          case 2:    // JPG
 102                  $img_source = imagecreatefromjpeg($img_file);
 103                  break;
 104          
 105          case 3:    // PNG
 106                  $img_source = imagecreatefrompng($img_file);
 107                  break;
 108      
 109      }
 110      
 111      imagecopyresized($new_img, $img_source, 0, 0, 0, 0, $img_width, $img_height, $img_info[0], $img_info[1]);
 112      
 113      header('Content-type: '.$img_mimetype);
 114      
 115      switch($img_target) {
 116  
 117          case 'jpg':        imagejpeg($new_img, '', $img_quality);
 118                          break;
 119      
 120          case 'png':        imagepng($new_img, '', 9);
 121                          break;
 122      
 123          case 'gif':        imagegif($new_img);
 124                          break;
 125  
 126      }
 127      
 128      imagedestroy($new_img);
 129      imagedestroy($img_source);
 130      
 131  } else {
 132  
 133      // error / no image
 134      header ('Content-type: image/png');
 135      $new_img = imagecreatetruecolor(75, 20);
 136      $text_color = imagecolorallocate($new_img, 255, 255, 255);
 137      imagestring($new_img, 1, 5, 5,  "Image Error", $text_color);
 138      imagepng($new_img, '', 9);
 139      imagedestroy($new_img);
 140  
 141  
 142  }
 143  
 144  ?>


Generated: Sun Jan 29 16:31:14 2012 Cross-referenced by PHPXref 0.7.1