php image convert type function upload tutorial.pdf

4
Image Convert Type Function Upload Tutorial Learn how to convert images from one type to another using the GD library functions. In this specific video example we convert any uploaded image file to a jpg. my_file.php <form enctype="multipart/formdata" method="post" action="image_ upload _script.php"> Choose your file here: <input name="uploaded_file" type="file"/><br /><br /> <input type="submit" value="Upload It"/> </form> image_upload_script.php <?php // Access the $_FILES global variable for this specific file being uploaded // and create local PHP variables from the $_FILES array of information $fileName = $_FILES["uploaded_file"]["name"]; // The file name $fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"]; // File in the PHP tmp folder $fileType = $_FILES["uploaded_file"]["type"]; // The type of file it is $fileSize = $_FILES["uploaded_file"]["size"]; // File size in bytes $fileErrorMsg = $_FILES["uploaded_file"]["error"]; // 0 for false... and 1 for true $fileName = preg_replace('#[^az.09]#i', '', $fileName); //

Upload: bharat

Post on 04-Oct-2015

19 views

Category:

Documents


8 download

TRANSCRIPT

  • 3/13/2015 PHPImageConvertTypeFunctionUploadTutorial

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

    searchDevelopPHP.com

    PHP ImageProcessing ImageConvertTypeFunctionUploadTutorial

    Image Convert Type Function Upload Tutorial

    LearnhowtoconvertimagesfromonetypetoanotherusingtheGDlibraryfunctions.Inthisspecificvideoexampleweconvertanyuploadedimagefiletoajpg.

    my_file.php

    Chooseyourfilehere:

    image_upload_script.php

  • 3/13/2015 PHPImageConvertTypeFunctionUploadTutorial

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

    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)

    }

  • 3/13/2015 PHPImageConvertTypeFunctionUploadTutorial

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

    //EndConverttoJPGFunction

    //Displaythingstothepagesoyoucanseewhatishappeningfor

    testingpurposes

    echo"Thefilenamed$fileNameuploaded

    successfuly."

    echo"Itis$fileSizebytesinsize."

    echo"Itisan$fileTypetypeoffile."

    echo"Thefileextensionis$fileExt"

    echo"TheErrorMessageoutputforthisuploadis:$fileErrorMsg"

    ?>

    ak_php_img_lib_1.0.php

  • 3/13/2015 PHPImageConvertTypeFunctionUploadTutorial

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

    $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/search.php?f=vid&q=Uploadhttp://www.developphp.com/Table-of-Contentshttp://www.developphp.com/search.php?f=vid&q=Imagehttp://youtube.com/subscription_center?add_user=flashbuildinghttp://www.webintersect.com/http://www.developphp.com/hosting.phphttp://www.developphp.com/terms_of_use.phphttp://www.developphp.com/search.php?f=vid&q=Functionhttp://www.developphp.com/testimonials.phphttp://www.developphp.com/search.php?f=vid&q=Typehttp://www.developphp.com/search.php?f=vid&q=Convert