PHP - Redimensionar imagenes al subirlas

 
Vista:

Redimensionar imagenes al subirlas

Publicado por Pablo (11 intervenciones) el 31/07/2011 01:07:17
Hola a todos, tengo un script que es de clasificados y los usuarios pueden subir una serie de fotos de sus articulos, pero el inconveniente de esto es que si suben fotos muy pesadas a la hora de cargar la página se hace demasiado lento. Lo que yo queria era ver de que forma pueden subir imagnes pero cuando lo hagan, estas automaticamente se redimensionen a un tamaño predeterminado, por ejemplo 640x480.

En mi script las imagenes se almacenan en una carpeta llamada re_images y cada usuario tiene su tabla en la base de datos y ahí mismo se guarda el nonmbre de la imágen dentro de (images)

Estas son las lineas del código para subir las imagenes:

if(isset($_POST[s1]))
{
if(!empty($_FILES[images][name][0]))
{
while(list($key,$value) = each($_FILES[images][name]))
{
if(!empty($value))
{
$NewImageName = $t."_offer_".$value;
copy($_FILES[images][tmp_name][$key], "re_images/".$NewImageName);

$MyImages[] = $NewImageName;
}
}

if(!empty($MyImages))
{
$ImageStr = implode("|", $MyImages);
}

}


Desde ya muchas gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Redimensionar imagenes al subirlas

Publicado por Bruno Quintana (25 intervenciones) el 01/08/2011 13:54:49
Lo llamas resize.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
 
/*
	Version 1.0 Created by: Ryan Stemkoski
	Questions or comments: ryan@ipowerplant.com
	Visit us on the web at: http://www.ipowerplant.com
	Purpose:  This script can be used to resize one or more images.  It will save the file to a directory and output the path to that directory which you 
			  can display or write to a databse. 
	
	TO USE, SET: 
		$filename = image to be resized
		$newfilename = added to filename to for each use to keep from overwriting images created example thumbnail_$filename is how it will be saved.
		$path = where the image should be stored and accessed. 
		$newwidth = resized width could be larger or smaller
		$newheight = resized height could be larger or smaller
		
	SAMPLE OF FUNCTION: makeimage('image.jpg','fullimage_','imgs/',250,250)
	
	Include the file containing the function in your document and simply call the function with the correct parameters and your image will be resized.
	
*///poner while aqui hasta el final
 
//IMAGE RESIZE FUNCTION FOLLOW ABOVE DIRECTIONS
function makeimage($filename,$newfilename,$path,$newwidth,$newheight) {
 
	//SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . )
	$image_type = strstr($filename, '.');
 
	//SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
		switch($image_type) {
			case '.jpg':
				$source = imagecreatefromjpeg($filename);
				break;
			case '.png':
				$source = imagecreatefrompng($filename);
				break;
			case '.gif':
				$source = imagecreatefromgif($filename);
				break;
			default:
				echo("Error Invalid Image Type");
				die;
				break;
			}
 
	//CREATES THE NAME OF THE SAVED FILE
	$file = $newfilename . $filename;
 
	//CREATES THE PATH TO THE SAVED FILE
	$fullpath = $path . $file;
 
	//FINDS SIZE OF THE OLD FILE
	list($width, $height) = getimagesize($filename);
 
	//CREATES IMAGE WITH NEW SIZES
	$thumb = imagecreatetruecolor($newwidth, $newheight);
 
	//RESIZES OLD IMAGE TO NEW SIZES
	imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
 
	//SAVES IMAGE AND SETS QUALITY || NUMERICAL VALUE = QUALITY ON SCALE OF 1-100
	imagejpeg($thumb, $fullpath, 60);
 
	//CREATING FILENAME TO WRITE TO DATABSE
	$filepath = $fullpath;
 
	//RETURNS FULL FILEPATH OF IMAGE ENDS FUNCTION
	return $filepath;
 
}
 
?>



index.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
</head>
<? include("resize.php"); ?>
<body>
<table width="813" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="231" height="19"><strong>ORGINAL IMAGE:</strong></td>
  </tr>
  <tr>
    <td height="19"><img src="test.jpg"></td>
  </tr>
</table>
<p>&nbsp;</p>
<table width="813" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="231" height="19"><strong>RESIZED IMAGE:</strong></td>
  </tr>
  <tr>
    <td height="19"><img src="<? echo(makeimage('test.jpg','thumbnail_','imgs/',100,100)); ?>"></td>
  </tr>
</table>
</body>
</html>



Luego lo estudias y podras adaptar a lo que necesites, yo actualmente estoy buscando la manera de subir imagenes con 'uploadify' que es un script que sube imagenes.

Pero sube solo las imagenes no crea thum ni nada, ya que es para subir archivos en general, ahora estoy intentando hacer a alguna manera para redimensionar todas e ir separando por distintas carpetas... jeje creo que tendre que contratar un programador! un saludo :)
Espero te haya servido
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar