Mam ten kod, który pomaga mi tworzyć obraz JPEG, teraz potrzebuję stworzyć obraz, PNG JPG lub cokolwiek to jest!
Dokonałem kilku modyfikacji i oto ostatni wynik:
function tamano_nuevo_foto($im_or, $ancho_nv, $dir_nv) {
$ext = pathinfo($im_or, PATHINFO_EXTENSION);
$datos = getimagesize($im_or);
$ancho = $datos[0];
$alto = $datos[1];
if ($ancho > $ancho_nv) { //Si la imagen no lelga al máximo no la tocamos.
$prop = $alto / $ancho;
$alto_nv = round($ancho_nv * $prop); //Sacamos la nueva altura
} else {
$ancho_nv = $ancho;
$alto_nv = $alto;
}
$im_nv = imagecreatetruecolor($ancho_nv, $alto_nv);
imagecopyresampled($im_nv, $img, 0, 0, 0, 0, $ancho_nv, $alto_nv, $ancho, $alto);
switch ($ext) {
case 'jpg':
$img = imagecreatefromjpeg($im_or);
break;
case 'jpeg':
$img = imagecreatefromjpeg($im_or);
break;
case 'png':
$img = imagecreatefrompng($im_or);
break;
case 'gif':
$img = imagecreatefromgif($im_or);
break;
default:
$img = imagecreatefromjpeg($im_or);
}
imagedestroy($im_nv);
}
Podczas gdy oryginalny kod to:
function tamano_nuevo_foto($im_or, $ancho_nv, $dir_nv) {
$img = imagecreatefromstring($im_or);
$datos = getimagesize($im_or);
$ancho = $datos[0];
$alto = $datos[1];
if ($ancho > $ancho_nv) { //Si la imagen no lelga al máximo no la tocamos.
$prop = $alto / $ancho; /* Calculo la proporcion entre la or y la nv (lo miltiplicamos por mil para evitar problemas con decimales */
$alto_nv = round($ancho_nv * $prop); //Sacamos la nueva altura
} else {
$ancho_nv = $ancho;
$alto_nv = $alto;
}
$im_nv = imagecreatetruecolor($ancho_nv, $alto_nv);
imagecopyresampled($im_nv, $img, 0, 0, 0, 0, $ancho_nv, $alto_nv, $ancho, $alto);
imagejpeg($im_nv, $dir_nv);
imagedestroy($im_nv);
}
Funkcja tworzy tak:
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetPath = str_replace('//','/',$targetPath);
$targetFile = $targetPath . $_FILES['Filedata']['name'];
tamano_nuevo_foto($tempFile, 800, $targetFile);
2 odpowiedzi
Masz na myśli, że chciałeś tak:
function tamano_nuevo_foto($im_or, $width_nv, $dir_nv) {
list($width, $height, $type, $attr) = getimagesize($im_or);
$x_ratio = $width_nv / $width;
if($width <= $width_nv) {
$width_nv = $width;
$height_nv = $height;
} else {
$height_nv = ceil($height * $x_ratio);
}
$im_nv = imagecreatetruecolor($width_nv, $height_nv);
switch ($type) {
case '3':
imagealphablending($im_nv, false);
imagesavealpha($im_nv, true);
$im_or = imagecreatefrompng($im_or);
imagealphablending($im_or, true);
header("Content-Type: image/png");
imagecopyresampled($im_nv, $im_or, 0, 0, 0, 0, $width_nv, $height_nv, $width, $height);
imagepng($im_nv, $dir_nv);
break;
case '1':
$im_or = imagecreatefromgif($im_or);
header("Content-Type: image/gif");
imagecopyresampled($im_nv, $im_or, 0, 0, 0, 0, $width_nv, $height_nv, $width, $height);
imagegif($im_nv, $dir_nv);
break;
default:
$im_or = imagecreatefromjpeg($im_or);
header("Content-Type: image/jpeg");
imagecopyresampled($im_nv, $im_or, 0, 0, 0, 0, $width_nv, $height_nv, $width, $height);
imagejpeg($im_nv, $dir_nv, 100);
}
imagedestroy($im_nv);
}
Wygląda na to, że używasz zmiennej $img
przed utworzeniem zasobu obrazu:
imagecopyresampled($im_nv, $img, 0, 0, 0, 0, $ancho_nv, $alto_nv, $ancho, $alto);
//^^ using $img
switch ($ext)
{//creating img here...
case 'jpg':
$img = imagecreatefromjpeg($im_or);
break;
case 'jpeg':
$img = imagecreatefromjpeg($im_or);
break;
case 'png':
$img = imagecreatefrompng($im_or);
break;
case 'gif':
$img = imagecreatefromgif($im_or);
break;
default:
$img = imagecreatefromjpeg($im_or);
}
Po prostu przesuń imagecopyresampled
w dół, zaraz za przełącznikiem. BTW, ten przełącznik można napisać o wiele mniej nieporęcznie:
switch ($ext)
{//creating img here...
case 'png':
$img = imagecreatefrompng($im_or);
break;
case 'gif':
$img = imagecreatefromgif($im_or);
break;
default://no cases for jpg or jpeg means default, which creates from jpeg
$img = imagecreatefromjpeg($im_or);
}
imagecopyresampled($im_nv, $img, 0, 0, 0, 0, $ancho_nv, $alto_nv, $ancho, $alto);
Jeśli z jakiegoś powodu chcesz zachować te jpeg
przypadki, nie musisz pisać tego samego kodu dwa razy, dzięki przejściu:
switch ($ext)
{//creating img here...
case 'jpeg':
//you can even add some code specifically for the jpeg
// extention, that will not be executed if the extention is jpg
//if you omit the break at the end, the next case will be executed, too
case 'jpg':
$img = imagecreatefromjpeg($im_or);
break;
case 'png':
$img = imagecreatefrompng($im_or);
break;
case 'gif':
$img = imagecreatefromgif($im_or);
break;
default://no cases for jpg or jpeg means default, which creates from jpeg
$img = imagecreatefromjpeg($im_or);
}
imagecopyresampled($im_nv, $img, 0, 0, 0, 0, $ancho_nv, $alto_nv, $ancho, $alto);
Podobne pytania
Nowe pytania
php
PHP to szeroko stosowany, wysokopoziomowy, dynamiczny, zorientowany obiektowo i interpretowany język skryptowy przeznaczony głównie do tworzenia stron WWW po stronie serwera. Używane w przypadku pytań dotyczących języka PHP.