You are on page 1of 4

3/13/2015

PHPImageConvertTypeFunctionUploadTutorial
searchDevelopPHP.com

PHP

ImageProcessing

ImageConvertTypeFunctionUploadTutorial

Image Convert Type Function Upload Tutorial


LearnhowtoconvertimagesfromonetypetoanotherusingtheGDlibraryfunctions.In
thisspecificvideoexampleweconvertanyuploadedimagefiletoajpg.
my_file.php
<formenctype="multipart/formdata"method="post"
action="image_upload_script.php">
Chooseyourfilehere:
<inputname="uploaded_file"type="file"/><br/><br/>
<inputtype="submit"value="UploadIt"/>
</form>

image_upload_script.php
<?php
//Accessthe$_FILESglobalvariableforthisspecificfilebeing
uploaded
//andcreatelocalPHPvariablesfromthe$_FILESarrayof
information
$fileName=$_FILES["uploaded_file"]["name"]//Thefilename
$fileTmpLoc=$_FILES["uploaded_file"]["tmp_name"]//Fileinthe
PHPtmpfolder
$fileType=$_FILES["uploaded_file"]["type"]//Thetypeoffile
itis
$fileSize=$_FILES["uploaded_file"]["size"]//Filesizein
bytes
$fileErrorMsg=$_FILES["uploaded_file"]["error"]//0for
false...and1fortrue
$fileName=preg_replace('#[^az.09]#i','',$fileName)//
http://www.developphp.com/video/PHP/ImageConvertTypeFunctionUploadTutorial

1/4

3/13/2015

PHPImageConvertTypeFunctionUploadTutorial

filterthe$filename
$kaboom=explode(".",$fileName)//Splitfilenameintoan
arrayusingthedot
$fileExt=end($kaboom)//Nowtargetthelastarrayelementto
getthefileextension
//STARTPHPImageUploadErrorHandling

if(!$fileTmpLoc){//iffilenotchosen
echo"ERROR:Pleasebrowseforafilebeforeclickingthe
uploadbutton."
exit()
}elseif($fileSize>5242880){//iffilesizeislargerthan5
Megabytes
echo"ERROR:Yourfilewaslargerthan5Megabytesinsize."
unlink($fileTmpLoc)//RemovetheuploadedfilefromthePHP
tempfolder
exit()
}elseif(!preg_match("/.(gif|jpg|png)$/i",$fileName)){
//Thisconditionisonlyifyouwishtoallowuploadingof
specificfiletypes
echo"ERROR:Yourimagewasnot.gif,.jpg,or.png."
unlink($fileTmpLoc)//RemovetheuploadedfilefromthePHP
tempfolder
exit()
}elseif($fileErrorMsg==1){//iffileuploaderrorkeyis
equalto1
echo"ERROR:Anerroroccuredwhileprocessingthefile.Try
again."
exit()
}
//ENDPHPImageUploadErrorHandling

//Placeitintoyour"uploads"foldermowusingthe
move_uploaded_file()function
$moveResult=move_uploaded_file($fileTmpLoc,
"uploads/$fileName")
//Checktomakesurethemoveresultistruebeforecontinuing
if($moveResult!=true){
echo"ERROR:Filenotuploaded.Tryagain."
exit()
}
//Includethefilethathousesallofourcustomimagefunctions
include_once("ak_php_img_lib_1.0.php")
//StartUniversalImageResizingFunction
$target_file="uploads/$fileName"
$resized_file="uploads/resized_$fileName"
$wmax=500
$hmax=500
ak_img_resize($target_file,$resized_file,$wmax,$hmax,
$fileExt)
//EndUniversalImageResizingFunction
//StartConverttoJPGFunction
if(strtolower($fileExt)!="jpg"){
$target_file="uploads/resized_$fileName"
$new_jpg="uploads/resized_".$kaboom[0].".jpg"
ak_img_convert_to_jpg($target_file,$new_jpg,$fileExt)
}
http://www.developphp.com/video/PHP/ImageConvertTypeFunctionUploadTutorial

2/4

3/13/2015

PHPImageConvertTypeFunctionUploadTutorial

//EndConverttoJPGFunction
//Displaythingstothepagesoyoucanseewhatishappeningfor
testingpurposes
echo"Thefilenamed<strong>$fileName</strong>uploaded
successfuly.<br/><br/>"
echo"Itis<strong>$fileSize</strong>bytesinsize.<br/><br
/>"
echo"Itisan<strong>$fileType</strong>typeoffile.<br/><br
/>"
echo"Thefileextensionis<strong>$fileExt</strong><br/><br
/>"
echo"TheErrorMessageoutputforthisuploadis:$fileErrorMsg"
?>

ak_php_img_lib_1.0.php
<?php
//RESIZEFUNCTION
//Functionforresizinganyjpg,gif,orpngimagefiles
functionak_img_resize($target,$newcopy,$w,$h,$ext){
list($w_orig,$h_orig)=getimagesize($target)
$scale_ratio=$w_orig/$h_orig
if(($w/$h)>$scale_ratio){
$w=$h*$scale_ratio
}else{
$h=$w/$scale_ratio
}
$img=""
$ext=strtolower($ext)
if($ext=="gif"){
$img=imagecreatefromgif($target)
}elseif($ext=="png"){
$img=imagecreatefrompng($target)
}else{
$img=imagecreatefromjpeg($target)
}
$tci=imagecreatetruecolor($w,$h)
//imagecopyresampled(dst_img,src_img,dst_x,dst_y,src_x,
src_y,dst_w,dst_h,src_w,src_h)
imagecopyresampled($tci,$img,0,0,0,0,$w,$h,$w_orig,
$h_orig)
if($ext=="gif"){
imagegif($tci,$newcopy)
}elseif($ext=="png"){
imagepng($tci,$newcopy)
}else{
imagejpeg($tci,$newcopy,84)
}
}
//THUMBNAIL(CROP)FUNCTION
//Functionforcreatingatruethumbnailcroppingfromanyjpg,
gif,orpngimagefiles
functionak_img_thumb($target,$newcopy,$w,$h,$ext){
list($w_orig,$h_orig)=getimagesize($target)
$src_x=($w_orig/2)($w/2)
$src_y=($h_orig/2)($h/2)
$ext=strtolower($ext)
http://www.developphp.com/video/PHP/ImageConvertTypeFunctionUploadTutorial

3/4

3/13/2015

PHPImageConvertTypeFunctionUploadTutorial

$img=""
if($ext=="gif"){
$img=imagecreatefromgif($target)
}elseif($ext=="png"){
$img=imagecreatefrompng($target)
}else{
$img=imagecreatefromjpeg($target)
}
$tci=imagecreatetruecolor($w,$h)
imagecopyresampled($tci,$img,0,0,$src_x,$src_y,$w,$h,
$w,$h)
if($ext=="gif"){
imagegif($tci,$newcopy)
}elseif($ext=="png"){
imagepng($tci,$newcopy)
}else{
imagejpeg($tci,$newcopy,84)
}
}
//IMAGECONVERTFUNCTION
//FunctionforconvertingGIFsandPNGstoJPGuponupload
functionak_img_convert_to_jpg($target,$newcopy,$ext){
list($w_orig,$h_orig)=getimagesize($target)
$ext=strtolower($ext)
$img=""
if($ext=="gif"){
$img=imagecreatefromgif($target)
}elseif($ext=="png"){
$img=imagecreatefrompng($target)
}
$tci=imagecreatetruecolor($w_orig,$h_orig)
imagecopyresampled($tci,$img,0,0,0,0,$w_orig,$h_orig,
$w_orig,$h_orig)
imagejpeg($tci,$newcopy,84)
}
?>

Image

Convert

Type

Function

Upload

TermsOfUse TableofContents Testimonials Hosting Subscribe DeveloperQ+A Top

http://www.developphp.com/video/PHP/ImageConvertTypeFunctionUploadTutorial

4/4

You might also like