Quiero hacer un thumbnails de imagenes de 200x200 en extensión JPG, pero respetandome en esos 200 de un thumbnails la imagen.
Tengo un script hecho pero creo que habría que modificar algo, algunas imagenes me lo hace bien pero otras no me lo respteta.
Me pueden ayudar?
Código PHP:
<?php
function compresion($IMEX,$comp=200)
{
# Condicion
# **********************************************************
($comp == 60) ? $alto1 = 60 : $alto1 = 200;
($comp == 60) ? $folder = 'comp60/' : $folder = 'comp200/';
# Valores predefinidos
# **********************************************************
$CalidadCompresion = 100;
$DIR = "imagen/";
$IMEX1 = $DIR."/".$folder.$IMEX;
# Extensión del archivos
# **********************************************************
$extension = explode(".",$IMEX);
$ext = count($extension)-1;
# COMPRIMIR JPG
# **********************************************************
if(strtoupper($extension[$ext]) == strtoupper("jpg") or strtoupper($extension[$ext]) == strtoupper("jpeg"))
{
if ($image = @imagecreatefromjpeg("$DIR$IMEX")){}else{@copy ($DIR.$IMEX,$IMEX1);}
$info = getimagesize($DIR.$IMEX);
//imagen horizontal
$xpos = ceil(($info[0]-$info[1]) /2);
$ypos = 0;
$width = $info[1];
$height = $info[1];
$image_new = imagecreatetruecolor($alto1, $alto1);
$bgcolor = imagecolorallocate($image_new, 255, 255, 255);
imagefilledrectangle($image_new, 0, 0, $alto1, $alto1, $bgcolor);
imagealphablending($image_new, true);
//Copiamos la imagen original con las nuevas dimensiones
imagecopyresampled($image_new, $image, 0, 0, $xpos, $ypos, $alto1, $alto1, $width, $height);
//Guardamos la nueva imagen como jpg con una calidad del 85%
imagejpeg($image_new, $IMEX1, $CalidadCompresion);
return "jpg";
}
}
compresion("4016803101048-1.jpg",200);
compresion("4971850182276.jpg",200);
compresion("4242006180812.JPG",200);
compresion("5025232543267.JPG",200);
compresion("caja_default01.JPG",200);
?>