මේකෙන් කියන්නේ අලුත් වැඩක් ගැන..php බාවිතා කරලා අපිට image uploading,resizing,renaming,type conversations වගේ වැඩ ගොඩක් කරන්න පුළුවන් ..ඒ හැම එකම එකින් එක මේ ලිපි පෙළ හරහා කියනවා..මේකට මුලින්ම අපිට file upload කරන්න විදිහක් හදාගන්න ඕන ..file uploading application එකක් හදාගන්නේ කොහොමද?
මුලින්ම මේකට file upload කරන interface එක හදාගන්න ඕන.
index.php
<html> <head></head> <body> <form enctype="multipart/form-data" method ="post" action="upload.php"> choose file here: <input name="image" type= "file"> <input type="submit" value="upload"> </form> </body> </html>
සරල html form එකක් බාවිතා කරලා upload link එකක් සහ submit button එකක් දාගෙන තියෙනවා.submit button එක click කරාම upload.php කියන file එකට තමා load වෙන්නේ ..මේ file එකේ තමා image එක upload කරන්න ඕන code එක තියෙන්නේ.
upload.php
<?php $fileName= $_FILES["image"]["name"]; $fileTmp = $_FILES["image"]["tmp_name"]; $fileType = $_FILES["image"]["type"]; $fileSize = $_FILES["image"]["size"]; $fileError = $_FILES["image"]["error"]; if (!$fileTmp){ echo "ERROR:Please select a file"; exit(); }elseif (!preg_match("/\.(gif|jpg|png)$/i", $fileName)){ echo "ERROR,File should be .gif,.jpg or .png"; exit(); }elseif ($fileError == 1){ echo "ERROR: Error occured.."; exit(); } $res = move_uploaded_file($fileTmp, "images/$fileName"); if ($res != true){ echo "ERROR:File not uploaded!!"; exit(); } echo "File $fileName uploaded successfully";
මේක සරල code එකක්..මුලින්ම $_FILES superglobal variable එක හරහා පිලිවෙලින් file name,tempory location,file type,file size,file errors vriables වලට assign කරගන්න එක ..
$fileName= $_FILES["image"]["name"]; $fileTmp = $_FILES["image"]["tmp_name"]; $fileType = $_FILES["image"]["type"]; $fileSize = $_FILES["image"]["size"]; $fileError = $_FILES["image"]["error"];
$_FILES[‘image’][‘tmp_name’]; මේකෙන් වෙන්නේ image එක process කරනකම් තාවකාලිකව server එකේ රදවාගෙන තියෙන temporary location එක ගන්න එක.
preg_match(“/\.(gif|jpg|png)$/i”, $fileName) මේකෙන් වෙන්නේ අපි upload කරන image එක .gif , .jpg , .png ද බලන එක.
move_uploaded_file($fileTmp, “images/$fileName”); මේකෙන් වෙන්නේ server එකේ තිබ්බ file එක images කියන local folder එකට move කරන එක..මෙහිදී upload කරද්දී ලබාදෙන file name එකෙන්ම images folder එකට move වෙනවා..
folder structure
Image resizing
මේ වැඩේට අපිට php library එකක් බාවිතා කරන්න ඕන.. GD Library එක තමා බාවිතා කරන්නේ..
<?php var_dump(gd_info()); ?>
මේ code එකනේ පුළුවන් දැනට install වෙලා තියෙන GD Library එකේ විස්තර බලාගන්න..
image එක resize කරන්න අපිට වෙනම function එකක් හදාගන්න පුළුවන්..
මුලින්ම අපිට upload කරපු images වල file extension එක ගන්න ඕන ..ඒකට upload.php එකේම අලුතෙන් variables දෙකක් බාවිතා කරලා extension එක ගන්න පුළුවන් ..
$file = explode(".", $fileName); $extension = $file[1];
මේකෙන් වෙන්නේ upload කරන file name එක “.” එකෙන් separate වෙන එක..output එක array එකක් නිසා array index 1 වල තමා හැම වෙලේම extension එක තියෙන්නේ.
දැන් resize function එක බලමු..
<?php function resize($img,$newimg,$resWidth,$resHeight,$exte){ list($width,$height)= getimagesize($img); $scale_ratio = $width/$height; if (($resWidth/$resHeight)>$scale_ratio){ $resWidth = $resHeight*$scale_ratio; }else{ $resHeight= $resWidth/$scale_ratio; } }
$img – resize කරන image එකේ path එක
$newimg – අන්තිමට image එක save වෙන path එක
$maxWidth – maximum width
$maxHeight – maximum height
$exte – file extension
image එකේ aspect ratio එක ගණනය කරලා තමයි resize width එක සහ height එක හොයාගන්නේ..(link එකෙන් aspect ratio එක ගැන බලන්න පුළුවන්)
ඊළගට මේ function එකේම ඉතුරු ටික.extension එක අනුව image එක resize කරලා save කරන්නේ මේ කොටසෙන් .
$image = ""; if ($exte == "gif" ||$exte == "GIF" ) { $image = imagecreatefromgif($img); }else if ($exte == "png" ||$exte == "PNG") { $image = imagecreatefrompng($img); }else{ $image = imagecreatefromjpeg($img); } $layout = imagecreatetruecolor($resWidth, $resHeight); imagecopyresampled($layout, $img, 0,0,0, 0, $resWidth, $resHeight, $width, $height); imagejpeg($layout,$newimg,80);
imagecreatefromgif,imagecreatefrompng,imagecreatefromjpeg,imagecreatetruecolor,imagecopyresampled,imagejpeg මේ හැම function එකක්ම GD Library එකට අදාළ functions.
මුලින්ම file extension එක අනුව මොන file එකක්ද හැදෙන්න ඕන කියන එක check කරගන්න ඕන ඊළගට imagecreatetruecolor($resWidth, $resHeight) function එකෙන් වෙන්නේ කලින් ගණනය කරගත්ත resize height එක සහ resize width එකට layout එකක් හදාගන්න එක..
imagecopyresampled function එකට parameters 10 ක් ඕන …
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
$dst_image = destination image (resize copy)
$src _image = source image(identified image ==> $image)(not original image)
$dst_x,$dst_y = destination x and y values
$src_x,$src_y = source x and y values
$dst_w,$dst_h = destination width and height(resize height and resize width)
$src_w,$src_h = source height and width(original height and width)
අන්තිම function එක imagejpeg.. මේකෙන් වෙන්නේ resize file එක save කරන එක..
imagejpeg($layout,$newimg,80);
හදාගත්ත resize image එක $newimg desitnation එකේ quality=80
අවසාන resize.php file එකේ code එක මේක..
<?php function resize($img,$newimg,$resWidth,$resHeight,$exte){ list($width,$height)= getimagesize($img); $scale_ratio = $width/$height; if (($resWidth/$resHeight)>$scale_ratio){ $resWidth = $resHeight*$scale_ratio; }else{ $resHeight= $resWidth/$scale_ratio; } $image = ""; if ($exte == "gif" ||$exte == "GIF" ) { $image = imagecreatefromgif($img); }else if ($exte == "png" ||$exte == "PNG") { $image = imagecreatefrompng($img); }else{ $image = imagecreatefromjpeg($img); } $layout = imagecreatetruecolor($resWidth, $resHeight); imagecopyresampled($layout, $image, 0,0,0, 0, $resWidth, $resHeight, $width, $height); imagejpeg($layout,$newimg,80); }
අවසාන වශයෙන් අදාළ අගයන් සමග function එක call කරලා output එක ගන්න පුළුවන්..
upload.php එකේ file එක upload කරාට පස්සේ resize function එක call කරන්න ඕන..
include_once 'resize.php'; $target_file = "images/$fileName"; $resize_copy = "images/resize/thumb_$fileName"; $maxWidth =300; $maxHeight=200; resize($target_file, $resize_copy, $maxWidth, $maxHeight, $extension);
අවසාන upload.php file එක..
<?php $fileName= $_FILES["image"]["name"]; $fileTmp = $_FILES["image"]["tmp_name"]; $fileType = $_FILES["image"]["type"]; $fileSize = $_FILES["image"]["size"]; $fileError = $_FILES["image"]["error"]; $file = explode(".", $fileName); $extension = $file[1]; if (!$fileTmp){ echo "ERROR:Please select a file"; exit(); }elseif (!preg_match("/\.(gif|jpg|png)$/i", $fileName)){ echo "ERROR,File should be .gif,.jpg or .png"; exit(); }elseif ($fileError == 1){ echo "ERROR: Error occured.."; exit(); } $res = move_uploaded_file($fileTmp, "images/$fileName"); if ($res != true){ echo "ERROR:File not uploaded!!"; exit(); } include_once 'resize.php'; $target_file = "images/$fileName"; $resize_copy = "images/resize/thumb_$fileName"; $maxWidth =300; $maxHeight=200; resize($target_file, $resize_copy, $maxWidth, $maxHeight, $extension); echo "File $fileName uploaded successfully";
දැන් සාමාන්ය විදිහට image එකක් upload කරලා බැලුවොත් .. image එක crop නොවී සම්පුර්ණ image එකම තියෙන නමුත් ලබාදෙන අගයට අනුව resize image එකක් resize folder එකේ store වෙනවා..
මේකට අදාළ සම්පුර්ණ codes ටික මේ link එකෙන් ………..
(https://github.com/rajithst/RST-S-BLOG)
crop image එකක original image එක හොයාගන්නේ කොහොමද? type conversation,watermaking මේ ටික ඊලග post එකෙන්……..comment එකක් දාගෙනම යන්න..