-
- /**
- * desription compressed image
- * @param sting $imgsrc image path
- * @param string $imgdst save path after compression
- */
- function image_png_size_add($imgsrc,$imgdst){
- list($width,$height,$type)=getimagesize($imgsrc);
- $new_width = ($width>600?600:$width)*0.9;
- $new_height =($height>600?600:$height)*0.9;
- switch($type){
- case 1:
- $giftype=check_gifcartoon($imgsrc);
- if($giftype){
- header('Content-Type:image/gif');
- $image_wp=imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefromgif($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- imagejpeg($image_wp, $imgdst,75);
- imagedestroy($image_wp);
- }
- break;
- case 2:
- header('Content-Type:image/jpeg');
- $image_wp=imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefromjpeg($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- imagejpeg($image_wp, $imgdst,75);
- imagedestroy($image_wp);
- break;
- case 3:
- header('Content-Type:image/png');
- $image_wp=imagecreatetruecolor($new_width, $new_height);
- $image = imagecreatefrompng($imgsrc);
- imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
- imagejpeg($image_wp, $imgdst,75);
- imagedestroy($image_wp);
- break;
- } // bbs.it-home.org
- }
- /**
- * desription determines whether it is a gif animation
- * @param sting $image_file image path
- * @return boolean t yes f no
- */
- function check_gifcartoon($image_file){
- $fp = fopen($image_file,'rb');
- $image_head = fread($fp,1024);
- fclose($fp);
- return preg_match("/".chr(0x21).chr(0xff).chr(0x0b).'NETSCAPE2.0'."/",$image_head)?false:true;
- }
- ?>
复制代码
Example 2:
-
- /*
- Function: Adjust image size or generate thumbnails
- Return: True/False
- Parameters:
- $Image The image that needs to be adjusted (including path)
- $Dw=450 when adjusting Maximum width; absolute width when thumbnailing
- $Dh=450 Maximum height when adjusting; absolute height when thumbnailing
- $Type=1 1, adjust size; 2, generate thumbnail
- $path='img/';/ /path
- $phtypes=array(
- 'img/gif',
- 'img/jpg',
- 'img/jpeg',
- 'img/bmp',
- 'img/pjpeg',
- 'img/x-png '
- );
- Function Img($Image,$Dw=450,$Dh=450,$Type=1){
- IF(!File_Exists($Image)){
- Return False;
- }
- //Generate if necessary Thumbnail, copy the original image and re-assign it to $Image
- IF($Type!=1){
- Copy($Image,Str_Replace(".","_x.",$Image));
- $Image= Str_Replace(".","_x.",$Image);
- }
- //Get the file type and create different objects according to different types
- $ImgInfo=GetImageSize($Image);
- Switch($ImgInfo[2 ]){
- Case 1:
- $Img = @ImageCreateFromGIF($Image);
- Break;
- Case 2:
- $Img = @ImageCreateFromJPEG($Image);
- Break;
- Case 3:
- $Img = @ImageCreateFromPNG( $Image);
- Break;
- }
- //If the object is not created successfully, it means it is not an image file
- IF(Empty($Img)){
- //If there is an error when generating the thumbnail, you need to delete it Copied file
- IF($Type!=1){Unlink($Image);}
- Return False;
- }
- //If the resize operation is performed, then
- IF($Type==1){
- $w= ImagesX($Img);
- $h=ImagesY($Img);
- $width = $w;
- $height = $h;
- IF($width>$Dw){
- $Par=$Dw/$width;
- $width=$Dw;
- $height=$height*$Par;
- IF($height>$Dh){
- $Par=$Dh/$height;
- $height=$Dh;
- $width=$width *$Par;
- }
- }ElseIF($height>$Dh){
- $Par=$Dh/$height;
- $height=$Dh;
- $width=$width*$Par;
- IF($width> $Dw){
- $Par=$Dw/$width;
- $width=$Dw;
- $height=$height*$Par;
- }
- }Else{
- $width=$width;
- $height=$height ;
- }
- $nImg = ImageCreateTrueColor($width,$height); //Create a new true color canvas
- ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$ h);//Resample copy part of the image and resize it
- ImageJpeg ($nImg,$Image); //Output the image to the browser or file in JPEG format
- Return True;
- //If you are performing a thumbnail generation operation Then
- }Else{
- $w=ImagesX($Img);
- $h=ImagesY($Img);
- $width = $w;
- $height = $h;
- $nImg = ImageCreateTrueColor($Dw,$Dh );
- IF($h/$w>$Dh/$Dw){ //The height ratio is large
- $width=$Dw;
- $height=$h*$Dw/$w;
- $IntNH=$height- $Dh;
- ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
- }Else{ //The width ratio is large
- $height= $Dh;
- $width=$w*$Dh/$h;
- $IntNW=$width-$Dw;
- ImageCopyReSampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $Dh, $w, $h);
- }
- ImageJpeg ($nImg,$Image);
- Return True;
- }
- }
- ?>
-
- The file types allowed to be uploaded are:=implode(', ',$phtypes)?>< ;/form>
- if($_SERVER['REQUEST_METHOD']=='POST'){
- if (!is_uploaded_file($_FILES["photo"][tmp_name])){
- echo "The picture is not Exists";
- exit();
- }
- if(!is_dir('img')){//If the path does not exist, create it
- mkdir('img');
- }
- $upfile=$_FILES["photo" ];
- $pinfo=pathinfo($upfile["name"]);
- $name=$pinfo['basename'];//File name
- $tmp_name=$upfile["tmp_name"];
- $file_type=$ pinfo['extension'];//Get the file type
- $showphpath=$path.$name;
-
- if(in_array($upfile["type"],$phtypes)){
- echo "The file type does not match! ";
- exit();
- }
- if(move_uploaded_file($tmp_name,$path.$name)){
- echo "Success! ";
- Img($showphpath,100,800,2);
- }
- echo "";
- }
- ?>
- < ;/html>
Copy code
|