Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/02/2012, 20:23
vfu_u
 
Fecha de Ingreso: febrero-2012
Mensajes: 19
Antigüedad: 12 años, 3 meses
Puntos: 0
Redimensionar imagen al subir y guardar en carpeta especificada

Estuve viendo el excelente contenido que hay sobre el tema que quiero tratar en este topic, en esta página de forosdelweb.com:

http://www.forosdelweb.com/wiki/PHP:Manejando_imagenes_al_subirlas_al_servidor

Como sé poco de programación quisiera saber si me pueden ayudar a hacer un pequeña modificación en los siguientes scripts para hacer algo muy simple, enviar las imágenes a una carpeta especificada.

El problema que tuve es que los archivos se suben sólo a la carpeta donde se encuentran ambos scripts y me gustaría que se pueda enviar a otra porque estoy usando una galería dinámica que al detectar un archivo lo muestra haciendo que ambos archivos aparecan en blanco en la galería.

Los 2 archivos que elegí son estos:

ModifiedImage.php

Código PHP:
Ver original
  1. <?php
  2. /**
  3.  * File: ModifiedImage.php
  4.  * Author: Simon Jarvis
  5.  * Copyright: Simon Jarvis
  6.  * Date: Aug-11-06
  7.  * Original link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
  8.  *
  9.  * Last Modified Date: Aug-04-11
  10.  * Modified by: abimaelrc - http://www.forosdelweb.com/miembros/abimaelrc/
  11.  * Modified by: iviamontes - http://www.forosdelweb.com/miembros/iviamontes/
  12.  * Modified by: Triby - http://www.forosdelweb.com/miembros/triby/
  13.  * Gif transparency by iviamontes
  14.  * Watermark by abimaelrc/iviamontes
  15.  * ResizeToFit by Triby
  16.  */
  17.  
  18. class ModifiedImage{
  19.     private $_image;
  20.     private $_imageType;
  21.     private $_transparent;
  22.  
  23.     /**
  24.      * Original link: http://www.php.net/manual/es/function.imagecopymerge.php#92787
  25.      * PNG ALPHA CHANNEL SUPPORT for imagecopymerge();
  26.      * by Sina Salek
  27.      *
  28.      * Bugfix by Ralph Voigt (bug which causes it
  29.      * to work only for $src_x = $src_y = 0.
  30.      * Also, inverting opacity is not necessary.)
  31.      * 08-JAN-2011
  32.      **/
  33.     private function _imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){
  34.         $cut = imagecreatetruecolor($src_w, $src_h);
  35.         imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h);
  36.         imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h);
  37.         imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct);
  38.     }
  39.  
  40.     private function _setPositionWatermark($width, $height, $position = 'bottom right', $paddingH = 10, $paddingV = 10)
  41.     {
  42.         switch(strtolower($position)){
  43.             case 'top left':
  44.                 $h = $paddingH;
  45.                 $v = $paddingV;
  46.                 break;
  47.             case 'top center':
  48.                 $h = ($this->getWidth() / 2) - ($width / 2) - $paddingH;
  49.                 $v = $paddingV;
  50.                 break;
  51.             case 'top right':
  52.                 $h = $this->getWidth() - $width - $paddingH;
  53.                 $v = $paddingV;
  54.                 break;
  55.             case 'middle left':
  56.                 $h = $paddingH;
  57.                 $v = ($this->getHeight() / 2) - ($height / 2) - $paddingV;
  58.                 break;
  59.             case 'middle center':
  60.                 $h = ($this->getWidth() / 2) - ($width / 2) - $paddingH;
  61.                 $v = ($this->getHeight() / 2) - ($height / 2) - $paddingV;
  62.                 break;
  63.             case 'middle right':
  64.                 $h = $this->getWidth() - $width - $paddingH;
  65.                 $v = ($this->getHeight() / 2) - ($height / 2) - $paddingV;
  66.                 break;
  67.             case 'bottom left':
  68.                 $h = $paddingH;
  69.                 $v = $this->getHeight() - $height - $paddingV;
  70.                 break;
  71.             case 'bottom center':
  72.                 $h = ($this->getWidth() / 2) - ($width / 2) - $paddingH;
  73.                 $v = $this->getHeight() - $height - $paddingV;
  74.                 break;
  75.             default:
  76.                 $h = $this->getWidth() - $width - $paddingH;
  77.                 $v = $this->getHeight() - $height - $paddingV;
  78.         }
  79.         return array('horizontal'=>$h, 'vertical'=>$v);
  80.     }
  81.  
  82.     public function __construct($fileName=null, $transparent=false)
  83.     {
  84.         $this->setTransparent($transparent);
  85.  
  86.         if(!is_null($fileName)){
  87.             $this->load($fileName);
  88.         }
  89.     }
  90.  
  91.     public function setTransparent($bool)
  92.     {
  93.         $this->_transparent = (boolean)$bool;
  94.     }
  95.  
  96.     public function load($fileName)
  97.     {
  98.         $imageInfo = getimagesize($fileName);
  99.         $this->_imageType = $imageInfo[2];
  100.  
  101.         if($this->_imageType == IMAGETYPE_JPEG){
  102.             $this->_image = imagecreatefromjpeg($fileName);
  103.         }
  104.         elseif($this->_imageType == IMAGETYPE_GIF){
  105.             $this->_image = imagecreatefromgif($fileName);
  106.         }
  107.         elseif($this->_imageType == IMAGETYPE_PNG){
  108.             $this->_image = imagecreatefrompng($fileName);
  109.         }
  110.     }
  111.  
  112.     public function save($fileName, $compression = 75, $permissions = null)
  113.     {
  114.         if($this->_imageType == IMAGETYPE_JPEG){
  115.             imagejpeg($this->_image, $fileName, $compression);
  116.         }
  117.         elseif($this->_imageType == IMAGETYPE_GIF){
  118.             imagegif($this->_image, $fileName);
  119.         }
  120.         elseif($this->_imageType == IMAGETYPE_PNG){
  121.             imagepng($this->_image, $fileName);
  122.         }
  123.  
  124.         if(!is_null($permissions)) {
  125.             chmod($fileName, $permissions);
  126.         }
  127.     }
  128.  
  129.     public function output()
  130.     {
  131.         if($this->_imageType == IMAGETYPE_JPEG){
  132.             imagejpeg($this->_image);
  133.         }
  134.         elseif($this->_imageType == IMAGETYPE_GIF){
  135.             imagegif($this->_image);
  136.         }
  137.         elseif($this->_imageType == IMAGETYPE_PNG){
  138.             imagepng($this->_image);
  139.         }  
  140.     }
  141.  
  142.     public function getWidth()
  143.     {
  144.         return imagesx($this->_image);
  145.     }
  146.  
  147.     public function getHeight()
  148.     {
  149.         return imagesy($this->_image);
  150.     }
  151.  
  152.     public function resizeToHeight($height)
  153.     {
  154.         $ratio = $height / $this->getHeight();
  155.         $width = $this->getWidth() * $ratio;
  156.         $this->resize($width,$height);
  157.     }
  158.  
  159.     public function resizeToWidth($width)
  160.     {
  161.         $ratio = $width / $this->getWidth();
  162.         $height = $this->getHeight() * $ratio;
  163.         $this->resize($width, $height);
  164.     }
  165.  
  166.     public function scale($scale)
  167.     {
  168.         $width = $this->getWidth() * $scale / 100;
  169.         $height = $this->getHeight() * $scale / 100;
  170.         $this->resize($width, $height);
  171.     }
  172.  
  173.     public function resize($width, $height)
  174.     {
  175.         $newImage = imagecreatetruecolor($width, $height);
  176.         if($this->_imageType == IMAGETYPE_PNG && $this->_transparent === true){
  177.             imagealphablending($newImage, false);
  178.             imagesavealpha($newImage, true);
  179.             imagefilledrectangle($newImage, 0, 0, $width, $height, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
  180.         }
  181.         elseif($this->_imageType == IMAGETYPE_GIF && $this->_transparent === true){
  182.             $index = imagecolortransparent($this->_image);
  183.             if($index != -1 && $index != 255){
  184.                 $colors = imagecolorsforindex($this->_image, $index);
  185.                 $transparent = imagecolorallocatealpha($newImage, $colors['red'], $colors['green'], $colors['blue'], $colors['alpha']);
  186.                 imagefill($newImage, 0, 0, $transparent);
  187.                 imagecolortransparent($newImage, $transparent);
  188.             }
  189.         }
  190.         imagecopyresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  191.         $this->_image = $newImage;
  192.     }
  193.  
  194.     public function resizeToFit($width, $height, $margins = false, $hexBckColor = '000000') {
  195.         $ratioW = $width / $this->getWidth();
  196.         $ratioH = $height / $this->getHeight();
  197.         $ratio = ($margins === false) ? max($ratioW, $ratioH) : min($ratioW, $ratioH);
  198.         $newW = floor($this->getWidth() * $ratio);
  199.         $newH = floor($this->getHeight() * $ratio);
  200.  
  201.         $this->resize($newW, $newH);
  202.  
  203.         if($newW != $width || $newH != $height) {
  204.             $newImage = imagecreatetruecolor($width, $height);
  205.             imagefill($newImage, 0, 0, "0x$hexBckColor");
  206.  
  207.             $ox = ($newW > $width) ? floor(($newW - $width) / 2) : 0;
  208.             $oy = ($newH > $height) ? floor(($newH - $width) / 2) : 0;
  209.             $dx = ($newW < $width) ? floor(($width - $newW) / 2) : 0;
  210.             $dy = ($newH < $height) ? floor(($height - $newH) / 2) : 0;
  211.  
  212.             imagecopy($newImage, $this->_image, $dx, $dy, $ox, $oy, $newW, $newH);
  213.             $this->_image = $newImage;
  214.         }
  215.     }
  216.  
  217.     public function imgWatermark($img, $opacity = 100, $position = 'bottom right', $paddingH = 10, $paddingV = 10)
  218.     {
  219.         $iw = getimagesize($img);
  220.         $width = $iw[0];
  221.         $height = $iw[1];
  222.  
  223.         $p = $this->_setPositionWatermark($width, $height, $position, $paddingH, $paddingV);
  224.  
  225.         imagealphablending($this->_image, true);
  226.         $watermark = imagecreatefrompng($img);
  227.         $this->_imagecopymerge_alpha($this->_image, $watermark, $p['horizontal'], $p['vertical'], 0, 0, $width, $height, $opacity);
  228.         imagedestroy($watermark);
  229.  
  230.         return $this->_image;
  231.     }
  232.  
  233.     public function stringWatermark($string, $opacity = 100, $color = '000000', $position = 'bottom right', $paddingH = 10, $paddingV = 10){
  234.         $width = imagefontwidth(5) * strlen($string);
  235.         $height = imagefontwidth(5) + 10;
  236.  
  237.         $p = $this->_setPositionWatermark($width, $height, $position, $paddingH, $paddingV);
  238.  
  239.         $watermark = imagecreatetruecolor($width, $height);
  240.         imagealphablending($watermark, false);
  241.         imagesavealpha($watermark, true);
  242.         imagefilledrectangle($watermark, 0, 0, $width, $height, imagecolorallocatealpha($watermark, 255, 255, 255, 127));
  243.  
  244.         imagestring($watermark, 5, 0, 0, $string, "0x$color");
  245.         $this->_imagecopymerge_alpha($this->_image, $watermark, $p['horizontal'], $p['vertical'], 0, 0, $width, $height, $opacity);
  246.  
  247.         return $this->_image;
  248.     }
  249. }
  250.  
  251.  
  252. ?>

y por último este que lo llamo upload.php

Código PHP:
Ver original
  1. <?php
  2. if(!empty($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_OK) {
  3.     require_once 'ModifiedImage.php';
  4.  
  5.     $image = new ModifiedImage($_FILES['image']['tmp_name']);
  6.  
  7.     if($image->getHeight() > 400){
  8.         $image->resizeToHeight(400);
  9.         $h400 = 'its_paraguay_' . $_FILES['image']['name'];
  10.         $image->save($h400);
  11.     }
  12. ?>
  13. <a href="/imagenes/<?php echo $h400; ?>"></a>
  14. <?php } ?>
  15.  
  16. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
  17.     <input type="file" name="image" />
  18.     <input type="submit" name="submit" value="Upload" />
  19. </form>

Demás está decir que intenté cambiar la url en

Código PHP:
Ver original
  1. <a href="/imagenes/<?php echo $h400; ?>"></a>


del segundo archivo y no conseguí que envíe la imagen en la carpeta que quería.


Ojalá puedan ayudarme!

Saludos!