PHP ile resim boyutlandirmak ve resim upload etmek icin asagida kodlari kullanabilirsiniz.
PHP Resim Boyutlandirma Fonksiyonu:
function image_resize($type, $l, $w, $h)
{
if($type=="png") header("Content-type: image/png");
elseif($type=="jpg") header("Content-type: image/jpeg");
elseif($type=="gif") header("Content-type: image/gif");
if($type=="png") $imgs=imagecreatefrompng($l);
elseif($type=="jpg") $imgs=imagecreatefromjpeg($l);
elseif($type=="gif") $imgs=imagecreatefromgif($l);
$width=imagesx($imgs);
$height=imagesy($imgs);
$sheight = $h;
$swidth = $w;
$imgss = ImageCreateTrueColor($swidth,$sheight);
imagecopyresampled($imgss,$imgs,0,0,0,0,$swidth,$sheight,$width,$height);
if($type=="png") imagepng($imgss,$l);
elseif($type=="jpg") imagejpeg($imgss,$l);
elseif($type=="gif") imagegif($imgss,$l);
imagedestroy($imgs);
imagedestroy($imgss);
}
Gordugunuz iki fonksiyona 4 degisken gonderiliyor. Birincisi resmin turu, yani uzantisi ($type), ikincisi resmin server uzerindeki yeri($l), daha sonra resmin istenilen uzunlugu($w) ve istenilen yukseklik($h).
Resmi Servera upload etmek icin gereken kod:
upload($_POST['width'], $_POST['height'], $_FILES['img']);
function upload($h, $w, $photo)
{
if($photo['size']>2000000) return false; // Sayi kismina resmin buyuklugunu girerek ust limit belirliyebilirsiniz
$i = strlen($photo['name']);
$type = substr($photo['name'],($i-3),3); // resmin uzantisini aliyoruz
if($type<>"jpg" AND $type<>"gif" AND $type<>"png") return false; // resmin turunu kontrol ediyoruz
$randnum="uploaded"."_".time(); // resmi yeniden adlandiriyoruz.
$loc="images/".$randnum.".".$type.""; // resmin kopyalanacagi yeri belirtiyoruz.
copy($photo['tmp_name'],$loc); // Resmi kopyaliyoruz.
image_resize($image,$type,$loc,$w,$h); // Yeniden boyutlandirmak icin yukardaki fonksiyonumuza gonderiyoruz.
$image = $randnum.".".$type;
if(file_exists($loc))
// Resmin yuklenip yuklemedigini kontrol ettikten sonra istedigimiz islemi gerceklestirebilirsiniz.
}
Resim yuklemek icin gerekli HTML form sayfasi :
<form action="upload.PHP" method="post" enctype="multipart/form-data">
<table border="1" cellspacing="0" cellpadding="0" width="100%">
<tr>
<td colspan="2">Yeni resim</td>
</tr>
<tr>
<td>Title : </td>
<td><input type="file" name="img"></td>
</tr>
<tr>
<td>Width : </td>
<td><input type="text" name="width"></td>
</tr>
<tr>
<td>Height : </td>
<td><input type="text" name="height"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="submit" value="YUKLE">
</td>
</tr>
</table>
</form>










