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
<?php /** * File: ModifiedImage.php * Author: Simon Jarvis * Copyright: Simon Jarvis * Date: Aug-11-06 * Original link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php * * Last Modified Date: Aug-04-11 * Modified by: abimaelrc - http://www.forosdelweb.com/miembros/abimaelrc/ * Modified by: iviamontes - http://www.forosdelweb.com/miembros/iviamontes/ * Modified by: Triby - http://www.forosdelweb.com/miembros/triby/ * Gif transparency by iviamontes * Watermark by abimaelrc/iviamontes * ResizeToFit by Triby */ class ModifiedImage{ private $_image; private $_imageType; private $_transparent; /** * Original link: http://www.php.net/manual/es/function.imagecopymerge.php#92787 * PNG ALPHA CHANNEL SUPPORT for imagecopymerge(); * by Sina Salek * * Bugfix by Ralph Voigt (bug which causes it * to work only for $src_x = $src_y = 0. * Also, inverting opacity is not necessary.) * 08-JAN-2011 **/ private function _imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ } private function _setPositionWatermark($width, $height, $position = 'bottom right', $paddingH = 10, $paddingV = 10) { case 'top left': $h = $paddingH; $v = $paddingV; break; case 'top center': $h = ($this->getWidth() / 2) - ($width / 2) - $paddingH; $v = $paddingV; break; case 'top right': $h = $this->getWidth() - $width - $paddingH; $v = $paddingV; break; case 'middle left': $h = $paddingH; $v = ($this->getHeight() / 2) - ($height / 2) - $paddingV; break; case 'middle center': $h = ($this->getWidth() / 2) - ($width / 2) - $paddingH; $v = ($this->getHeight() / 2) - ($height / 2) - $paddingV; break; case 'middle right': $h = $this->getWidth() - $width - $paddingH; $v = ($this->getHeight() / 2) - ($height / 2) - $paddingV; break; case 'bottom left': $h = $paddingH; $v = $this->getHeight() - $height - $paddingV; break; case 'bottom center': $h = ($this->getWidth() / 2) - ($width / 2) - $paddingH; $v = $this->getHeight() - $height - $paddingV; break; default: $h = $this->getWidth() - $width - $paddingH; $v = $this->getHeight() - $height - $paddingV; } } public function __construct($fileName=null, $transparent=false) { $this->setTransparent($transparent); $this->load($fileName); } } public function setTransparent($bool) { $this->_transparent = (boolean)$bool; } public function load($fileName) { $this->_imageType = $imageInfo[2]; if($this->_imageType == IMAGETYPE_JPEG){ } elseif($this->_imageType == IMAGETYPE_GIF){ } elseif($this->_imageType == IMAGETYPE_PNG){ } } public function save($fileName, $compression = 75, $permissions = null) { if($this->_imageType == IMAGETYPE_JPEG){ } elseif($this->_imageType == IMAGETYPE_GIF){ } elseif($this->_imageType == IMAGETYPE_PNG){ } } } public function output() { if($this->_imageType == IMAGETYPE_JPEG){ } elseif($this->_imageType == IMAGETYPE_GIF){ } elseif($this->_imageType == IMAGETYPE_PNG){ } } public function getWidth() { } public function getHeight() { } public function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } public function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getHeight() * $ratio; $this->resize($width, $height); } public function scale($scale) { $width = $this->getWidth() * $scale / 100; $height = $this->getHeight() * $scale / 100; $this->resize($width, $height); } public function resize($width, $height) { if($this->_imageType == IMAGETYPE_PNG && $this->_transparent === true){ imagefilledrectangle($newImage, 0, 0, $width, $height, imagecolorallocatealpha($newImage, 255, 255, 255, 127)); } elseif($this->_imageType == IMAGETYPE_GIF && $this->_transparent === true){ if($index != -1 && $index != 255){ $transparent = imagecolorallocatealpha($newImage, $colors['red'], $colors['green'], $colors['blue'], $colors['alpha']); } } imagecopyresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->_image = $newImage; } public function resizeToFit($width, $height, $margins = false, $hexBckColor = '000000') { $ratioW = $width / $this->getWidth(); $ratioH = $height / $this->getHeight(); $this->resize($newW, $newH); if($newW != $width || $newH != $height) { $this->_image = $newImage; } } public function imgWatermark($img, $opacity = 100, $position = 'bottom right', $paddingH = 10, $paddingV = 10) { $width = $iw[0]; $height = $iw[1]; $p = $this->_setPositionWatermark($width, $height, $position, $paddingH, $paddingV); $this->_imagecopymerge_alpha($this->_image, $watermark, $p['horizontal'], $p['vertical'], 0, 0, $width, $height, $opacity); return $this->_image; } public function stringWatermark($string, $opacity = 100, $color = '000000', $position = 'bottom right', $paddingH = 10, $paddingV = 10){ $p = $this->_setPositionWatermark($width, $height, $position, $paddingH, $paddingV); imagefilledrectangle($watermark, 0, 0, $width, $height, imagecolorallocatealpha($watermark, 255, 255, 255, 127)); $this->_imagecopymerge_alpha($this->_image, $watermark, $p['horizontal'], $p['vertical'], 0, 0, $width, $height, $opacity); return $this->_image; } } ?>
y por último este que lo llamo upload.php
Código PHP:
Ver original
<?php require_once 'ModifiedImage.php'; $image = new ModifiedImage($_FILES['image']['tmp_name']); if($image->getHeight() > 400){ $image->resizeToHeight(400); $h400 = 'its_paraguay_' . $_FILES['image']['name']; $image->save($h400); } ?> <a href="/imagenes/<?php echo $h400; ?>"></a> <?php } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <input type="file" name="image" /> <input type="submit" name="submit" value="Upload" /> </form>
Demás está decir que intenté cambiar la url en
Código PHP:
Ver original
<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!