Ver Mensaje Individual
  #18 (permalink)  
Antiguo 02/06/2011, 10:53
Avatar de abimaelrc
abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: redimensionar imagen externa y guardarla en la BD

Bueno trate esto
Código PHP:
Ver original
  1. <?php
  2. /**
  3.  * File: ResizePicture.php
  4.  * Author: Simon Jarvis
  5.  * Copyright: Simon Jarvis
  6.  * Date: 08/11/06
  7.  * Original link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  8.  *
  9.  * Modified Date: 05/14/11
  10.  */
  11.  
  12. class ResizePicture {
  13.    
  14.     private $_image;
  15.     private $_imageType;
  16.  
  17.     public function __construct($fileName=null)
  18.     {
  19.         if(!is_null($fileName)){
  20.             $this->load($fileName);
  21.         }
  22.     }
  23.  
  24.     public function load($fileName)
  25.     {
  26.         $imageInfo = getimagesize($fileName);
  27.         $this->_imageType = $imageInfo[2];
  28.  
  29.         if($this->_imageType == IMAGETYPE_JPEG){
  30.             $this->_image = imagecreatefromjpeg($fileName);
  31.         }
  32.         elseif($this->_imageType == IMAGETYPE_GIF){
  33.             $this->_image = imagecreatefromgif($fileName);
  34.         }
  35.         elseif($this->_imageType == IMAGETYPE_PNG){
  36.             $this->_image = imagecreatefrompng($fileName);
  37.         }
  38.     }
  39.  
  40.     public function save($fileName, $compression=75, $permissions=null)
  41.     {
  42.         if($this->_imageType == IMAGETYPE_JPEG){
  43.             imagejpeg($this->_image, $fileName, $compression);
  44.         }
  45.         elseif($this->_imageType == IMAGETYPE_GIF){
  46.             imagegif($this->_image, $fileName);        
  47.         }
  48.         elseif($this->_imageType == IMAGETYPE_PNG){
  49.             imagepng($this->_image, $fileName);
  50.         }
  51.  
  52.         if(!is_null($permissions)) {
  53.             chmod($fileName, $permissions);
  54.         }
  55.     }
  56.  
  57.     public function output()
  58.     {
  59.         if($this->_imageType == IMAGETYPE_JPEG){
  60.             imagejpeg($this->_image);
  61.         }
  62.         elseif($this->_imageType == IMAGETYPE_GIF){
  63.             imagegif($this->_image);        
  64.         }
  65.         elseif($this->_imageType == IMAGETYPE_PNG){
  66.             imagepng($this->_image);
  67.         }  
  68.     }
  69.  
  70.     public function getWidth()
  71.     {
  72.         return imagesx($this->_image);
  73.     }
  74.  
  75.     public function getHeight()
  76.     {
  77.         return imagesy($this->_image);
  78.     }
  79.  
  80.     public function getImageType()
  81.     {
  82.         switch($this->_imageType){
  83.             case IMAGETYPE_JPEG:
  84.                 $imageType = 'image/jpeg';
  85.                 break;
  86.             case IMAGETYPE_GIF:
  87.                 $imageType = 'image/gif';
  88.                 break;
  89.             case IMAGETYPE_PNG:
  90.                 $imageType = 'image/png';
  91.                 break;
  92.             default:
  93.                 $imageType = null;
  94.         }
  95.         return $imageType;
  96.     }
  97.  
  98.     public function resizeToHeight($height)
  99.     {
  100.         $ratio = $height / $this->getHeight();
  101.         $width = $this->getWidth() * $ratio;
  102.         $this->resize($width,$height);
  103.     }
  104.  
  105.     public function resizeToWidth($width)
  106.     {
  107.         $ratio = $width / $this->getWidth();
  108.         $height = $this->getHeight() * $ratio;
  109.         $this->resize($width, $height);
  110.     }
  111.  
  112.     public function scale($scale)
  113.     {
  114.         $width = $this->getWidth() * $scale / 100;
  115.         $height = $this->getHeight() * $scale / 100;
  116.         $this->resize($width, $height);
  117.     }
  118.  
  119.     public function resize($width, $height)
  120.     {
  121.         $newImage = imagecreatetruecolor($width, $height);
  122.         imagecopyresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  123.         $this->_image = $newImage;  
  124.     }      
  125. }
  126.  
  127. $url = 'http://ladyoak.com/images/stories/big/line-art-conversion.gif';
  128.  
  129. $image = new ResizePicture($url);
  130. $basename = basename($url);
  131. $original = $basename;
  132. $image->save($original);
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos