RE: [PHP-ES] Bajar tamaño de archivo JPG
| From: | Lazaro Rogel Aguirre | Date: | Tue, 25 Mar 2008 15:20:03 +0000 |
| Subject: | RE: [PHP-ES] Bajar tamaño de archivo JPG | ||
| Groups: | php.general.es | ||
| Request: | Send a blank email to general-es+get-62403@lists.php.net to get a copy of this message | ||
Hola Ezequiel, te mando una función (estaba en la red) que sirve para generar thumbnails de
150x150, pero se pueden cambiar esos valores; un archivo de 1.6 MB lo reduce hasta 6 KB.
Esta función tiene 2 detalles, 1) La altura tiene que ser menor que el ancho; 2) Todas las imagenes
de las que se va a generar un thumbnail, tienen que estar en una sola carpeta.
Ya dependera de ti si la utilizas tal cual o la adaptas a tus necesidades.
Como no te voy a querer si mi corazón azul es y mi piel dorada... siempre te querré...!!!!!>
From: dataneu@gmail.com> To: php-es@lists.php.net> Date: Tue, 25 Mar 2008 11:27:45 -0300>
Subject: Re: [PHP-ES] Bajar tamaño de archivo JPG> > El mar, 25-03-2008 a las 11:18 -0300,
Suarez escribió:> > > > No en realidad el problema esta cuando intento visualizar
varias imagenes, que al tener mas de 1 mb hace que tarde una eternidad en mostrar todas, esa
configuracion la tengo correcta, pero la idea es bajar el tamaño del archivo para que se pueda
mostrar sin tanto delay en la pagina. > > ahh.... entonces sí que suben. si intentas mostrar
varias imagenes de más> de 1mb se me hace que deberías redimensionarlas (tipo thumbnail)
primero> y luego mostrar la que solicitan en su tamaño original.> > un thumbnail
(~100x100), por pesado que sea no creo que ocupe más de 20> ó 30kb, eso agilizaría tu
página.> > saludos.> -- > Gustavo Pardo > Dataneu Argentina > Software & Web
Hosting > Mail: gustavo.pardo@dataneu.com > Web: http://dataneu.com/ > Tel.(+54 299) 489 6880 > Centenario, NQN
- Argentina> > > -- > PHP Spanish Localization Talk Mailing List
(http://www.php.net/)> To unsubscribe, visit: http://www.php.net/unsub.php>
_________________________________________________________________
Por ser usuario de Hotmail, ya tienes un Space ¡Aprovéchalo!
http://home.services.spaces.live.com/
<? ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // // // COPYRIGHT // // // // This software is developed and copyrighted by Xantus WEBdevelopment. // // // // // // TERMS OF USE // // // // This script may be used free for personal use. The copyright MUST remain intact. // // Please contact us if you want to have the copyright removed. // // // ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //// COMMON VARAIBLES ///// $imagefolder='./imagenes'; // folder where the images are storde $thumbsfolder='./thumbs'; // folder where the thumbnails are going to be stored $errorimage = 1; //toggel errorimages on/off (1/0) $prefix = "tn_"; // prefix of the thumbnails $max_width = 150; // maximum width of the thumbnails $max_height = 150; // maximum width of the thumbnails //////DO NOT CHANGE ANYTHING BELOW THIS LINE //// echo "<table cellspacing=0><tr><td align=center><a style='color: green; font-size: 11px; text-decoration: none;' href='http://www.xantus-webdevelopment.com'>Tool by - © Xantus WEBdevelopment</a></td></tr>"; @set_time_limit(0); if (!is_dir($thumbsfolder)){ mkdir($thumbsfolder, 0777); } if (!is_dir($thumbsfolder)){ echo "<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #4</b></font>: Could not create direcotry! Please create it manually!</td></tr></table>"; } $pics=directory($imagefolder,"jpg,jpeg,png,gif"); $pics=ditchtn($pics,$prefix); if ($pics[0]!="") { $time_start = microtime_float(); $ix = 0; foreach ($pics as $p) { createthumb($imagefolder."/".$p,$thumbsfolder."/".$prefix.$p,$max_width,$max_height,$errorimage); $ix++; } $time_end = microtime_float(); $time = $time_end - $time_start; echo "<tr><td style='border: 2px solid green;'> <b>DONE</b>, $ix thumbnails where created in ".round($time,4)." seconds! </td></tr></table>"; } else { echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #1</b></font>: There are no images in this folder!</td></tr></table>"); exit; } function ditchtn($arr,$thumbname) { foreach ($arr as $item) { $array = strstr($item, $thumbname); if (!$array[1]){ $tmparr[]=$item; } } return $tmparr; } function createthumb($name,$filename,$new_w,$new_h,$errorimage) { $system=explode(".",$name); if (strpos(strtolower($system[(count($system)-1)]), "jpg") !== FALSE ){ $src_img=@imagecreatefromjpeg($name); } if (strpos(strtolower($system[(count($system)-1)]), "jpeg") !== FALSE ){ $src_img=@imagecreatefromjpeg($name); } if (strpos(strtolower($system[(count($system)-1)]), "png") !== FALSE ){ $src_img=@imagecreatefrompng($name); } if (strpos(strtolower($system[(count($system)-1)]), "gif") !== FALSE ){ $src_img=@imagecreatefromgif($name); } if (!$src_img){ if (strpos(strtolower($system[(count($system)-1)]), "gif") !== FALSE ){ if($errorimage == 1){ $src_img = imagecreate ("150", "20"); $bgc = imagecolorallocate ($src_img, 255, 255, 255); $tc = imagecolorallocate ($src_img, 255, 0, 0); imagefilledrectangle ($src_img, 0, 0, 150, 20, $bgc); imagestring ($src_img, 1, 5, 5, "Error loading $name", $tc); imagejpeg($src_img,$filename); imagedestroy($src_img); echo "<tr><td style='border: 2px solid blue; border-bottom: 0px solid blue;'>"; echo "<a style='color: black; text-decoration: underline;' href='".$filename."'>Thumbnail</a> of <a style='color: black; text-decoration: underline;' href='".$name."'>".strtolower($name)."</a> <font color=red>unsuccessfully</font> created<br>"; echo "</td></tr>"; } else { echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #2</b></font>: Your GD lib does not support gif images!</td></tr></table>"); exit; } } else { echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #2</b></font>: Unknow filetype!</td></tr></table>"); exit; } } else { $old_x=imageSX($src_img); $old_y=imageSY($src_img); if ($old_x > $old_y){ $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y){ $thumb_w=$new_w; $thumb_h=$new_h; } if ($thumb_w == "" OR $thumb_h == ""){ echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #3</b></font>: Unexpected error!</td></tr></table>"); exit; } $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if (strpos(strtolower($system[(count($system)-1)]), "jpg") !== FALSE ){ imagejpeg($dst_img,$filename); } if (strpos(strtolower($system[(count($system)-1)]), "jpeg") !== FALSE ){ imagejpeg($dst_img,$filename); } if (strpos(strtolower($system[(count($system)-1)]), "png") !== FALSE ){ imagepng($dst_img,$filename); } if (strpos(strtolower($system[(count($system)-1)]), "gif") !== FALSE ){ imagegif($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); echo "<tr><td style='border: 2px solid blue; border-bottom: 0px solid blue;'>"; echo "<a style='color: black; text-decoration: underline;' href='".$filename."'>Thumbnail</a> of <a style='color: black; text-decoration: underline;' href='".$name."'>".strtolower($name)."</a> <font color=green>successfully</font> created<br>"; echo "</td></tr>"; } } function directory($dir,$filters) { $handle=opendir($dir); $files=array(); if ($filters == ""){ while(($file = readdir($handle))!==false){ $files[] = $file; } } else{ if ($filters != "") { $filters=explode(",",$filters); while (($file = readdir($handle))!==false){ for ($f=0;$f<count($filters);$f++){ $system=explode(".",$file); if (strtolower($system[(count(system))]) == strtolower($filters[$f])){ $files[] = $file; } } } } else { echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #3</b></font>: Unexpected error!</td></tr></table>"); exit; } } closedir($handle); return $files; } function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } ?>
<? ///////////////////////////////////////////////////////////////////////////////////////////////////////////// // // // COPYRIGHT // // // // This software is developed and copyrighted by Xantus WEBdevelopment. // // // // // // TERMS OF USE // // // // This script may be used free for personal use. The copyright MUST remain intact. // // Please contact us if you want to have the copyright removed. // // // ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //// COMMON VARAIBLES ///// $imagefolder='./imagenes'; // folder where the images are storde $thumbsfolder='./thumbs'; // folder where the thumbnails are going to be stored $errorimage = 1; //toggel errorimages on/off (1/0) $prefix = "tn_"; // prefix of the thumbnails $max_width = 150; // maximum width of the thumbnails $max_height = 150; // maximum width of the thumbnails //////DO NOT CHANGE ANYTHING BELOW THIS LINE //// echo "<table cellspacing=0><tr><td align=center><a style='color: green; font-size: 11px; text-decoration: none;' href='http://www.xantus-webdevelopment.com'>Tool by - © Xantus WEBdevelopment</a></td></tr>"; @set_time_limit(0); if (!is_dir($thumbsfolder)){ mkdir($thumbsfolder, 0777); } if (!is_dir($thumbsfolder)){ echo "<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #4</b></font>: Could not create direcotry! Please create it manually!</td></tr></table>"; } $pics=directory($imagefolder,"jpg,jpeg,png,gif"); $pics=ditchtn($pics,$prefix); if ($pics[0]!="") { $time_start = microtime_float(); $ix = 0; foreach ($pics as $p) { createthumb($imagefolder."/".$p,$thumbsfolder."/".$prefix.$p,$max_width,$max_height,$errorimage); $ix++; } $time_end = microtime_float(); $time = $time_end - $time_start; echo "<tr><td style='border: 2px solid green;'> <b>DONE</b>, $ix thumbnails where created in ".round($time,4)." seconds! </td></tr></table>"; } else { echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #1</b></font>: There are no images in this folder!</td></tr></table>"); exit; } function ditchtn($arr,$thumbname) { foreach ($arr as $item) { $array = strstr($item, $thumbname); if (!$array[1]){ $tmparr[]=$item; } } return $tmparr; } function createthumb($name,$filename,$new_w,$new_h,$errorimage) { $system=explode(".",$name); if (strpos(strtolower($system[(count($system)-1)]), "jpg") !== FALSE ){ $src_img=@imagecreatefromjpeg($name); } if (strpos(strtolower($system[(count($system)-1)]), "jpeg") !== FALSE ){ $src_img=@imagecreatefromjpeg($name); } if (strpos(strtolower($system[(count($system)-1)]), "png") !== FALSE ){ $src_img=@imagecreatefrompng($name); } if (strpos(strtolower($system[(count($system)-1)]), "gif") !== FALSE ){ $src_img=@imagecreatefromgif($name); } if (!$src_img){ if (strpos(strtolower($system[(count($system)-1)]), "gif") !== FALSE ){ if($errorimage == 1){ $src_img = imagecreate ("150", "20"); $bgc = imagecolorallocate ($src_img, 255, 255, 255); $tc = imagecolorallocate ($src_img, 255, 0, 0); imagefilledrectangle ($src_img, 0, 0, 150, 20, $bgc); imagestring ($src_img, 1, 5, 5, "Error loading $name", $tc); imagejpeg($src_img,$filename); imagedestroy($src_img); echo "<tr><td style='border: 2px solid blue; border-bottom: 0px solid blue;'>"; echo "<a style='color: black; text-decoration: underline;' href='".$filename."'>Thumbnail</a> of <a style='color: black; text-decoration: underline;' href='".$name."'>".strtolower($name)."</a> <font color=red>unsuccessfully</font> created<br>"; echo "</td></tr>"; } else { echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #2</b></font>: Your GD lib does not support gif images!</td></tr></table>"); exit; } } else { echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #2</b></font>: Unknow filetype!</td></tr></table>"); exit; } } else { $old_x=imageSX($src_img); $old_y=imageSY($src_img); if ($old_x > $old_y){ $thumb_w=$new_w; $thumb_h=$old_y*($new_h/$old_x); } if ($old_x < $old_y) { $thumb_w=$old_x*($new_w/$old_y); $thumb_h=$new_h; } if ($old_x == $old_y){ $thumb_w=$new_w; $thumb_h=$new_h; } if ($thumb_w == "" OR $thumb_h == ""){ echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #3</b></font>: Unexpected error!</td></tr></table>"); exit; } $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); if (strpos(strtolower($system[(count($system)-1)]), "jpg") !== FALSE ){ imagejpeg($dst_img,$filename); } if (strpos(strtolower($system[(count($system)-1)]), "jpeg") !== FALSE ){ imagejpeg($dst_img,$filename); } if (strpos(strtolower($system[(count($system)-1)]), "png") !== FALSE ){ imagepng($dst_img,$filename); } if (strpos(strtolower($system[(count($system)-1)]), "gif") !== FALSE ){ imagegif($dst_img,$filename); } imagedestroy($dst_img); imagedestroy($src_img); echo "<tr><td style='border: 2px solid blue; border-bottom: 0px solid blue;'>"; echo "<a style='color: black; text-decoration: underline;' href='".$filename."'>Thumbnail</a> of <a style='color: black; text-decoration: underline;' href='".$name."'>".strtolower($name)."</a> <font color=green>successfully</font> created<br>"; echo "</td></tr>"; } } function directory($dir,$filters) { $handle=opendir($dir); $files=array(); if ($filters == ""){ while(($file = readdir($handle))!==false){ $files[] = $file; } } else{ if ($filters != "") { $filters=explode(",",$filters); while (($file = readdir($handle))!==false){ for ($f=0;$f<count($filters);$f++){ $system=explode(".",$file); if (strtolower($system[(count(system))]) == strtolower($filters[$f])){ $files[] = $file; } } } } else { echo ("<tr><td style='border: 2px solid red;'><font color=red><b>ERROR #3</b></font>: Unexpected error!</td></tr></table>"); exit; } } closedir($handle); return $files; } function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } ?>