PHP implementation image upload and compression, _php tutorial

Source: Internet
Author: User

PHP implementation of image upload and compression,


This article explains the PHP image upload and compression of the implementation method, share to everyone for reference, the specific content as follows

Use to three files

    • connect.php: Connecting to a Database
    • test_upload.php: Execute SQL statement
    • upload_img.php: Upload images and compress

Three file codes are as follows:
To connect to a database: connect.php

<?php$db_host = '; $db _user = '; $db _psw = '; $db _name = '; $db _port = '; $sqlconn =new mysqli ($db _host, $db _user, $db _ps W, $db _name); $q = "Set names UTF8;"; $result = $sqlconn->query ($q), if (Mysqli_connect_errno ()) {printf ("Connect failed:%s\n", Mysqli_connect_error ()); Exit ();}? >

Execute SQL statement: test_upload.php

<?phprequire ("connect.php"); Require ("upload_img.php"); $real _img= $uploadfile; $small _img= $uploadfile _resize; $insert _sql = "INSERT INTO img (real_img,small_img) VALUES (?,?)"; $result = $sqlconn, prepare ($insert _sql), $result-Bind_param ("ss", $real _img, $small _img); $result Execute ();? >

upload image and compress :upload_img.php

<?php//settings File save directory $uploaddir = "upfiles/"; Set the allowed upload file type $type=array ("JPG", "gif", "BMP", "JPEG", "PNG"); Gets the file suffix name function fileext ($filename) {return substr (STRRCHR ($filename, '. '), 1);}//Generate random file name function random ($lengt  h) {$hash = ' cr-';  $chars = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';  $max = strlen ($chars)-1;  Mt_srand (Double) microtime () * 1000000);  for ($i = 0; $i < $length; $i + +) {$hash. = $chars [Mt_rand (0, $max)]; } return $hash; } $a =strtolower (Fileext ($_files[' filename ' [' name ']));  Determine the file type if (!in_array (Strtolower (fileext ($_files[' filename ' [' name '])), $type)) {$text =implode (",", $type); $ret _code=3;//file type errors $page _result= $text; $retArray = Array (' ret_code ' = = $ret _code, ' page_result ' = $page _result); $retJson = Json_encode ($retArray); Echo $retJson; return;}  Generate the file name of the target else{$filename =explode (".", $_files[' filename ' [' name ']);   do {$filename [0]=random (10);//Set the length of the random number $name =implode (".", $filename); $name 1= $name. " MCNCC ";  $uploadfile = $uploaddir. $name;  } while (File_exists ($uploadfile)); if (move_uploaded_file ($_files[' filename ' [' tmp_name '], $uploadfile)) {if (Is_uploaded_file ($_files[' filename '] ['  Tmp_name ']) {$ret _code=1;//upload failed} else {//upload succeeded $ret _code=0;} } $retArray = Array (' ret_code ' = = $ret _code); $retJson = Json_encode ($retArray); echo $retJson;} Compress picture $uploaddir_resize= "upfiles_resize/"; $uploadfile _resize= $uploaddir _resize $name;//$pic _width_max=120;//$ pic_height_max=90;//above and the following paragraphs can be used in conjunction with the annotation, you can make the picture according to the calculated proportions compressed $file_type=$_files["filename" [' Type '];function resizeimage ( $uploadfile, $maxwidth, $maxheight, $name) {//Get the current picture size $width = Imagesx ($uploadfile); $height = Imagesy ($uploadfile); $i = 0.5; Generate thumbnail size if ($width > $maxwidth) | | ($height > $maxheight))  {/* $widthratio = $maxwidth/$width;    $heightratio = $maxheight/$height;  if ($widthratio < $heightratio) {$ratio = $widthratio;  } else {$ratio = $heightratio;  } $newwidth = $width * $ratio; $newheight = $heighT * $ratio;  */$newwidth = $width * $i;  $newheight = $height * $i;   if (function_exists ("imagecopyresampled")) {$uploaddir _resize = Imagecreatetruecolor ($newwidth, $newheight);  Imagecopyresampled ($uploaddir _resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);   } else {$uploaddir _resize = imagecreate ($newwidth, $newheight);  Imagecopyresized ($uploaddir _resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);  } imagejpeg ($uploaddir _resize, $name); Imagedestroy ($uploaddir _resize); } else {imagejpeg ($uploadfile, $name);}} if ($_files["filename" [' size ']) {if ($file _type = = "Image/pjpeg" | | $file _type = = "Image/jpg" | $file _type = = "Image/jpeg") {//$im = Imagecreatefromjpeg ($_files[$upload _input_name][' Tmp_  Name ']); $im = Imagecreatefromjpeg ($uploadfile);  } elseif ($file _type = = "Image/x-png") {//$im = Imagecreatefrompng ($_files[$upload _input_name][' tmp_name ']); $im = Imagecreatefromjpeg ($uploadfile); } elseif ($file _type = = "Image/giF ") {//$im = Imagecreatefromgif ($_files[$upload _input_name][' tmp_name ')); $im = Imagecreatefromjpeg ($uploadfile); } else//default jpg {$im = Imagecreatefromjpeg ($uploadfile),} if ($im) {resizeimage ($im, $pic _width_max, $pic _height_max, $UPL   Oadfile_resize); Imagedestroy ($im); }}?>

Please change the information in the connect.php,test_upload.php according to the actual situation.

The above is the PHP implementation of the image upload and compression method, I hope that everyone's learning PHP program to help

Articles you may be interested in:

    • PHP picture upload class with picture display
    • Simple PHP Image upload Program
    • PHP Image upload Class Code
    • PHP image upload storage source and can preview
    • PHP Image Upload Code
    • thinkphp Image upload function sharing
    • Two examples of image compression implemented by PHP
    • How to implement PHP upload image and compress

http://www.bkjia.com/PHPjc/1084576.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084576.html techarticle PHP Implementation of image upload and compression, this article explains the PHP image upload and compression of the implementation method, share to everyone for your reference, the specific contents of the following use to three files CONNECT.P ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.