Home >Backend Development >PHP Tutorial >Use php and imagemagick to handle uploading and scaling of image files_PHP tutorial
Use php and imagemagick to handle uploading and scaling of image files. It is very simple to upload files with php, but if you want to scale the uploaded images, although you can use GD to do it, it requires more cumbersome processing. ImageMagick is an image processing package that is available in most Linux software packages. It contains many tools for image processing. It can convert image file formats and perform various processing on images. Among them, we will use its image scaling function. This is achieved through the convert tool in its software package. To call convert -geometry, please create an images directory in the current directory for the width x height source file scaled file, and make this directory writable by the web execution user. Used to store uploaded images and zoomed images. Below is a simple example, including image uploading and processing, which was tested under RedHat6.0+php3.0.12. uploadform.html:File upload form〈HTML> 〈HEAD> 〈TITLE>Select file〈/TITLE> 〈/HEAD> 〈BODY ALIGN="CENTER"> 〈FORM ENCTYPE="multipart/form-data" ACTION="upload. php3" METHOD=POST> Select image file: 〈INPUT NAME="image" TYPE="file"> 〈INPUT TYPE="submit" VALUE="Send File"> 〈/FORM> 〈/BODY> 〈/HTML> upload .php3: Process the uploaded image file 〈html> 〈head> 〈title>Process 〈/title> 〈/head> 〈body> 〈? $flag = "true"; if(isset($image) && $image && ($image_type = "image/gif" || $image_type = "image/png" || $image_type = "image/pjpeg")){ //Determine the format of the uploaded file, etc. $dest_image = "./images/". $image_name; if(@copy($image,$dest_image)){ //Copy the uploaded file to the images directory $small_image = "./images/small".$image_name; $exec_str = "/usr/bin/X11/ convert -geometry 100x100 ".$dest_image." ".$small_image; @exec($exec_str); //Scale the image}else $flag = "false"; }else{ $flag = "false"; } if( $flag == "false"){ echo "error〈p>"; echo "〈a href=DownloadFilesa2004-11-05["uploadform.html[">Reupload〈/a>"; }else{ echo "〈 image src=/DownloadFilesa2004-11-05["$small_image[">"; echo "〈image src=/DownloadFilesa2004-11-05".images$image_name[">"; } ?> 〈/body> 〈/html > If combined with the GetImageSize function, we can also control the zoom size. Combined with the database, image files can be indexed or stored. The function of ImageMagick is very powerful. It can not only zoom image files, but also perform flipping, format conversion and other functions. Readers can refer to the ImageMagick help file themselves.