Проблем с код

Kibork4o

Registered
Здравейте! Изполозвам този код за ъплоуд на аватари. Той оразмерява аватара до 150х150, но след като го оразмери ми излиза прозорец за сваляне на оразмерената снимка. Как да го преработя така, че оразмереният аватар да се сваля в определена папка на хоста ми, без на хората да им излиза прозорец за сваляне?
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if ($_FILES['image']) {
preg_match('/\.([A-Za-z]+?)$/', $_FILES['image']['name'], $matches);
$matches[1] = strtolower($matches[1]);
if ($matches[1] == 'png' && function_exists('imagecreatefrompng')
|| $matches[1] == 'jpg' && function_exists('imagecreatefromjpeg')
|| $matches[1] == 'jpeg' && function_exists('imagecreatefromjpeg')
|| $matches[1] == 'gif' && function_exists('imagecreatefromgif')
|| $matches[1] == 'bmp' && function_exists('imagecreatefromwbmp')) {
list ($owidth, $oheight) = getimagesize($_FILES['image']['tmp_name']);
if ($_POST['resizeby'] == 'height')
{
$nheight = 150;
$nwidth = $nheight / $oheight * $owidth;
$resized = imagecreatetruecolor($nwidth, $nheight);
}
else
{
$nwidth = 150;
$nheight = $nwidth / $owidth * $oheight;
$resized = imagecreatetruecolor($nwidth, $nheight);
}
if ($matches[1] == 'png')
$original = imagecreatefrompng($_FILES['image']['tmp_name']);
if ($matches[1] == 'jpg' || $matches[1] == 'jpeg')
$original = imagecreatefromjpeg($_FILES['image']['tmp_name']);
if ($matches[1] == 'gif')
$original = imagecreatefromgif($_FILES['image']['tmp_name']);
if ($matches[1] == 'bmp')
$original = imagecreatefromwbmp($_FILES['image']['tmp_name']);
imagecopyresampled($resized, $original, 0, 0, 0, 0, $nwidth, $nheight, $owidth, $oheight);
header('Content-Disposition: attachment; filename="'.$_FILES['image']['name'].'"');
header('Content-type: image/'. (($matches[1] == 'jpg') ? 'jpeg' : $matches[1]));
if ($matches[1] == 'png')
imagepng($resized);
if ($matches[1] == 'jpg' || $matches[1] == 'jpeg')
imagejpeg($resized);
if ($matches[1] == 'gif')
imagegif($resized);
if ($matches[1] == 'bmpg')
imagewbmp($resized);
exit();
}
else
$error = 'File type not supported!';
}
else
$error = 'No image uploaded!';
}
?>
Мерси предварително ;)
 
PHP:
			$source_pic = патя до картинката която искаш да оразмери;
			$destination_pic = каде да я запаметява и под какво име;
			$max_width = 143; //височина
			$max_height = 143;//широчина

			$src = imagecreatefromjpeg($source_pic);
			list($width,$height)=getimagesize($source_pic);

			$x_ratio = $max_width / $width;
			$y_ratio = $max_height / $height;

			if( ($width <= $max_width) && ($height <= $max_height) ){
				$tn_width = $width;
				$tn_height = $height;
			}elseif (($x_ratio * $height) < $max_height){
				$tn_height = ceil($x_ratio * $height);
				$tn_width = $max_width;
			}else{
				$tn_width = ceil($y_ratio * $width);
				$tn_height = $max_height;
			}

			$tmp=imagecreatetruecolor($tn_width,$tn_height);
			imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

			imagejpeg($tmp,$destination_pic,100);
			imagedestroy($src);
			imagedestroy($tmp);
позлвам този и съм много доволен само трябва да си го настой правилно :]
 
Мерси, goshkoy, имаш една бира от мен.
ПП: Ето как преработих моя сцрипт с помощта на твоя и сега е точно, както го исках. Освен, това обработва всякакви формати, а не само 'jpg'. Полозвай го, ако искаш. Поздрави!
<?php
$jpeg='xxx/dsfgdsf.jpeg';
$png='xxx/dsfgdsf.png';
$bmp='xxx/dsfgdsf.bmpg';
$gif='xxx/dsfgdsf.gif';
$jpg='xxx/dsfgdsf.jpeg';

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if ($_FILES['image']) {
preg_match('/\.([A-Za-z]+?)$/', $_FILES['image']['name'], $matches);
$matches[1] = strtolower($matches[1]);
if ($matches[1] == 'png' && function_exists('imagecreatefrompng')
|| $matches[1] == 'jpg' && function_exists('imagecreatefromjpeg')
|| $matches[1] == 'jpeg' && function_exists('imagecreatefromjpeg')
|| $matches[1] == 'gif' && function_exists('imagecreatefromgif')
|| $matches[1] == 'bmp' && function_exists('imagecreatefromwbmp')) {
list ($owidth, $oheight) = getimagesize($_FILES['image']['tmp_name']);
if ($_POST['resizeby'] == 'height')
{
$nheight = 150;
$nwidth = $nheight / $oheight * $owidth;
$resized = imagecreatetruecolor($nwidth, $nheight);
}
else
{
$nwidth = 150;
$nheight = $nwidth / $owidth * $oheight;
$resized = imagecreatetruecolor($nwidth, $nheight);
}
if ($matches[1] == 'png')
$original = imagecreatefrompng($_FILES['image']['tmp_name']);
if ($matches[1] == 'jpg' || $matches[1] == 'jpeg')
$original = imagecreatefromjpeg($_FILES['image']['tmp_name']);
if ($matches[1] == 'gif')
$original = imagecreatefromgif($_FILES['image']['tmp_name']);
if ($matches[1] == 'bmp')
$original = imagecreatefromwbmp($_FILES['image']['tmp_name']);
imagecopyresampled($resized, $original, 0, 0, 0, 0, $nwidth, $nheight, $owidth, $oheight);
if ($matches[1] == 'png')
imagepng($resized,$png,0);
if ($matches[1] == 'jpg' || $matches[1] == 'jpeg')
imagejpeg($resized,$jpeg,100);
if ($matches[1] == 'gif')
imagegif($resized,$gif,100);
if ($matches[1] == 'bmpg')
imagewbmp($resized,$bmp,100);
exit();
}
else
$error = 'File type not supported!';
}
else
$error = 'No image uploaded!';
}
?>
 

Back
Горе