tiko165 Posted June 6, 2014 at 02:30 PM Report #558469 Posted June 6, 2014 at 02:30 PM Tenho desenvolvido um sistema de upload, em que até a uns tempos, todos os uploads era realizados com fotografias na horizontal, convertidos para a resolução 1024x768. Ate que houve um contratempo de enviar fotografias na vertical. Antes de utilizar imagens verticais, tinha assim o codigo: $NewWidth1=1024; $NewHeight1 = 768; Depois, eu adequei um código, mas o mesmo não me é favorável: $NewWidth1=1024; $NewHeight1 = ($height/$width)*$NewWidth1; Pelo seguinte, imagens na horizontal podem ficar na mesma 1024x768, mas se faço upload de imagens verticais, fica por exemplo 1024x1500. Contudo gostava que as imagens verticais ficassem por exemplo 540x768, para numa visualização em slide, terem todas o mesmo "Height". Como poderei proceder??? 🙂
guarana1 Posted June 6, 2014 at 02:50 PM Report #558471 Posted June 6, 2014 at 02:50 PM (edited) Eu uso uma classe chamada SimpleImage.e dessa classe uso: a função maxareafill. Por exemplo, $image->maxareafill(1300,842, 255, 255, 255); O 1300,842 é o tamanho que eu quero que a imagem fique. 255,255,255 é a cor do fundo que vai ficar. Não sei como é que o teu slider funciona, mas as vezes basta isto. Podes usar o resizetowidth ou resizetoheight. Esta classe tem mais coisas, como o flip over, etc... Embora as versões mais novas já estão mais avançadas. Podes fazer overlay, para ficar por exemplo, com o logo do site em todas as fotos que fazes upload, também podes por efeitos, tipo blur,etc... Se tiveres duvidas avisa. if( isset($_POST['submit']) ) { $valor_total = uniqid(); include('simpleimage.php'); $image = new SimpleImage(); $image->load($_FILES['image'] ['tmp_name']); $image2 = $_FILES['image']['name']; $image->save('imagens/'.$valor_total.$image2.''); $image-> resizetowidth(1800); $query_select = "insert into galeria(nome,titulo,tema) values('".$valor_total.$image2."','".$_POST['titulo']."','".$_POST['tema']."')"; $result_select = mysql_query($query_select) or die(mysql_error()); } ?> Se não resultar, tens de usar o maxareafill(); Não importa o tamanho da imagem, ou se está em horizontal ou vertical, a imagem vai sempre ficar desse tamanho. O que acontece é que se a imagem não der para ser redimensioada para esse tamanho sem perder o canvas size, ele adiciona uma espécie de bordas, e ai ficas com a mesma imagem mas do tamanho que queres, sem esticamentos nem nada disso. Deixo aqui a classe para que não tenhas de procurar. <?php /** * File: SimpleImage.php * Author: Simon Jarvis * Modified by: Miguel Fermín * Based in: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details: * http://www.gnu.org/licenses/gpl.html */ class SimpleImage { var $image; var $image_type; function __construct($filename = null){ if (!empty($filename)) { $this->load($filename); } } function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if ($this->image_type == IMAGETYPE_JPEG) { $this->image = imagecreatefromjpeg($filename); } elseif ($this->image_type == IMAGETYPE_GIF) { $this->image = imagecreatefromgif($filename); } elseif ($this->image_type == IMAGETYPE_PNG) { $this->image = imagecreatefrompng($filename); } else { throw new Exception("The file you're trying to open is not supported"); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if ($image_type == IMAGETYPE_JPEG) { imagejpeg($this->image,$filename,$compression); } elseif ($image_type == IMAGETYPE_GIF) { imagegif($this->image,$filename); } elseif ($image_type == IMAGETYPE_PNG) { imagepng($this->image,$filename); } if ($permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG, $quality = 80) { if ($image_type == IMAGETYPE_JPEG) { header("Content-type: image/jpeg"); imagejpeg($this->image, null, $quality); } elseif ($image_type == IMAGETYPE_GIF) { header("Content-type: image/gif"); imagegif($this->image); } elseif ($image_type == IMAGETYPE_PNG) { header("Content-type: image/png"); imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = round($this->getWidth() * $ratio); $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = round($this->getHeight() * $ratio); $this->resize($width,$height); } function square($size) { $new_image = imagecreatetruecolor($size, $size); if ($this->getWidth() > $this->getHeight()) { $this->resizeToHeight($size); imagecolortransparent($new_image, imagecolorallocate($new_image, 0, 0, 0)); imagealphablending($new_image, false); imagesavealpha($new_image, true); imagecopy($new_image, $this->image, 0, 0, ($this->getWidth() - $size) / 2, 0, $size, $size); } else { $this->resizeToWidth($size); imagecolortransparent($new_image, imagecolorallocate($new_image, 0, 0, 0)); imagealphablending($new_image, false); imagesavealpha($new_image, true); imagecopy($new_image, $this->image, 0, 0, 0, ($this->getHeight() - $size) / 2, $size, $size); } $this->image = $new_image; } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getHeight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecolortransparent($new_image, imagecolorallocate($new_image, 0, 0, 0)); imagealphablending($new_image, false); imagesavealpha($new_image, true); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } function cut($x, $y, $width, $height) { $new_image = imagecreatetruecolor($width, $height); imagecolortransparent($new_image, imagecolorallocate($new_image, 0, 0, 0)); imagealphablending($new_image, false); imagesavealpha($new_image, true); imagecopy($new_image, $this->image, 0, 0, $x, $y, $width, $height); $this->image = $new_image; } function maxarea($width, $height = null) { $height = $height ? $height : $width; if ($this->getWidth() > $width) { $this->resizeToWidth($width); } if ($this->getHeight() > $height) { $this->resizeToheight($height); } } function cutFromCenter($width, $height) { if ($width < $this->getWidth() && $width > $height) { $this->resizeToWidth($width); } if ($height < $this->getHeight() && $width < $height) { $this->resizeToHeight($height); } $x = ($this->getWidth() / 2) - ($width / 2); $y = ($this->getHeight() / 2) - ($height / 2); return $this->cut($x, $y, $width, $height); } function maxareafill($width, $height, $red, $green, $blue){ $this->maxarea($width, $height); $new_image = imagecreatetruecolor($width, $height); $color_fill = imagecolorallocate($new_image, $red, $green, $blue); imagefill($new_image, 0, 0, $color_fill); imagecopyresampled($new_image, $this->image, floor(($width - $this->getWidth())/2), floor(($height-$this->getHeight())/2), 0, 0, $this->getWidth(), $this->getHeight(), $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } Como é que se usa? Assim: <?php include("SimpleImage.php"); include("bd.php"); //buscar nome da imagem $valor_total = uniqid(); $image2 = $_FILES['ImageFile']['name']; $image = new SimpleImage($_FILES['ImageFile'] ['tmp_name']); //processamento da imagem $image->maxareafill(1300,842, 255, 255, 255); $image->save('processed/'.$valor_total.$image2.''); // Insereção na bd $query_select = "insert into galeria(foto,cod) values('".$valor_total.$image2."','".$res['cod']."')"; $result_select = mysql_query($query_select) or die(mysql_error()); ?> Desculpa se te confundi com alguma coisa. Resumindo, tenta antes com o resizetowidth ou resizetoheigh. Se percisares das duas coisas ao mesmo tempo (altura e comprimento) usa o maxareafill. Edited June 6, 2014 at 03:24 PM by guarana1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now