PHP generates a picture thumbnail function

Source: Internet
Author: User
Tags array imagejpeg php and return trim

The specific usage is as follows:

$a =thumbimage (' 1.jpg ', 200,100, ' 2.jpg '), and/or 1.jpg generates a thumbnail with a width of 200, a height of 100, and is saved as a 2.jpg
print_r ($a);

Print results:

Array (
[Status] => 0
[File] => 1.jpg
[Width] => 200
[Height] => 200
[Savepath] => 2.jpg
[INFO] => thumbnail image generation success
)

$a =thumbimage (' 1.jpg ', 200,100);//1.jpg to generate a width of 200, high 100 thumbnail, and directly overwrite 1.jpg, do not generate new pictures

The function code is as follows:

<?php/* *php a function *www to generate a picture thumbnail. alixixi.com/function Thumbimage ($file, $thumbWidth, $thumbHeight, $savePath = ', $isCut =true, $quality =100) {$result =
	Array (' status ' =>0, ' file ' => ', ' width ' =>0, ' height ' =>0, ' savepath ' => ', ' info ' => '); if (!file_exists ($file)) {return array (' status ' =>1, ' file ' => ', ' width ' =>0, ' height ' =>0, ' Savepath ' => ')
	', ' info ' => ' picture file does not exist ');
	//detection file type $fp =fopen ($file, ' RB '); $str =fread ($FP, 2);
	Read Only 2 bytes fclose ($fp);
	$str = @unpack ("C2chars", $str);
	$typeCode =intval ($str [' chars1 ']. $str [' chars2 ']);
	$fileType = ';
		Switch ($typeCode) {case 255216: $fileType = ' jpg ';
		Break
		Case 7173: $fileType = ' gif ';
		Break
		Case 6677: $fileType = ' bmp ';
		Break
		Case 13780: $fileType = ' png ';
		Break
		Default: $fileType = $typeCode;
	Break
	} if ($str [' chars1 ']== '-1 ' && $str [' chars2 ']== ' -40 ') {$fileType = ' jpg ';
	} if ($str [' chars1 ']== ' -119 ' && $str [' chars2 ']== ') {$fileType = ' png '; } if (!in_array ($fileType, array (' jpg ', ' gif', ' BMP ', ' png ')} {return array (' status ' =>2, ' file ' => ', ' width ' =>0, ' height ' =>0, ' Savepath ' => ') ', '
	Info ' => ' picture file type is incorrect: '. $fileType);
	//Picture Scaling Process if ($fileType = = ' jpg ') {$im =imagecreatefromjpeg ($file);
	} if ($fileType = = ' gif ') {$im =imagecreatefromgif ($file);
	} if ($fileType = = ' png ') {$im =imagecreatefrompng ($file);
	if (empty ($savePath)) $savePath = $file;
	$width =imagesx ($im);
	$height =imagesy ($im); if ($width < $thumbWidth && $height < $thumbHeight) {return array (' status ' =>3, ' file ' => ', ' width ' =
	>0, ' height ' =>0, ' savepath ' => ', ' info ' => ' picture size less than the size of the generated thumbnail '); $ratio = $width/$height//The proportions of the actual image $thumbRatio = $thumbWidth/$thumbHeight;//The proportions of the changed image if ($isCut) {//Trim process if (function_
		Exists (' imagepng ') && (Str_replace ('. ', ', php_version) >=512)) {//For the PHP version is greater than 5.12 parameter changes after the processing $quality = 9;
			} if ($ratio >= $thumbRatio) {//High priority $newimg =imagecreatetruecolor ($thumbWidth, $thumbHeight); Imagecopyresampled ($newimg, $im, 0,0,0,0, $thumbWidth, $thumbHeight,(($height) * $thumbRatio), $height);
		Imagejpeg ($newimg, $savePath, $quality);
			} if ($ratio < $thumbRatio) {//Width first $newimg =imagecreatetruecolor ($thumbWidth, $thumbHeight);
			Imagecopyresampled ($newimg, $im, 0,0,0,0, $thumbWidth, $thumbHeight, $width, (($width)/$thumbRatio));
		Imagejpeg ($newimg, $savePath, $quality);
			}else{//does not trim process if ($ratio >= $thumbRatio) {$newimg =imagecreatetruecolor ($thumbWidth, ($thumbWidth)/$ratio);
			Imagecopyresampled ($newimg, $im, 0,0,0,0, $thumbWidth, ($thumbWidth)/$ratio, $width, $height);
		Imagejpeg ($newimg, $savePath, $quality);
			} if ($ratio < $thumbRatio) {$newimg =imagecreatetruecolor ($thumbHeight) * $ratio, $thumbHeight);
			Imagecopyresampled ($newimg, $im, 0,0,0,0, ($thumbHeight) * $ratio, $thumbHeight, $width, $height);
		Imagejpeg ($newimg, $savePath, $quality); } Imagedestroy ($im); alixixi.com/php-function/1007.html return Array (' status ' =>0, ' file ' => $file, ' width ' => $thumbWidth, ' height ' => $thumbHeight, ' Savepath ' => $savePath, ' info ' =&Gt; '
Thumbnail image generation Success '); }

Articles that you may be interested in

    • class method for creating thumbnail images in PHP
    • PHP generates a continuous number (letter) Array function range () analysis, PHP lottery program function
    • PHP extracts the birthday date in the ID number and the function to verify that it is a minor
    • PHP to generate a histogram (statistical chart) program
    • PHP Gets the list of all the pictures in the content and outputs the method
    • PHP generates random cipher functions
    • How to generate short URLs in PHP and how to implement them
    • PHP Build Verification Code function


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.