Home > Backend Development > PHP Tutorial > Image thumbnail program that supports file upload and has good compatibility_PHP tutorial

Image thumbnail program that supports file upload and has good compatibility_PHP tutorial

WBOY
Release: 2016-07-13 10:45:47
Original
858 people have browsed it

The following are three types of generated abbreviation codes. The compatibility is quite good. You can also customize the height and width.

function imageresize($srcfile,$tow,$toh,$tofile="")
{
if($tofile==""){ $tofile = $srcfile; }
$info = "";
$data = getimagesize($srcfile,$info);
switch ($data[2])
{
case 1:
if(!function_exists("imagecreatefromgif")){
echo "Your gd library cannot use images in gif format, please use jpeg or png format!Return";
exit();
}
$im = imagecreatefromgif($srcfile);
break;
case 2:
if(!function_exists("imagecreatefromjpeg")){
echo "Your gd library cannot use images in jpeg format, please use images in other formats!Return";
exit();
}
$im = imagecreatefromjpeg($srcfile);
break;
case 3:
$im = imagecreatefrompng($srcfile);
break;
}
$srcw=imagesx($im);
$srch=imagesy($im);
$towh=$tow/$toh;
$srcwh=$srcw/$srch;
if($towh<=$srcwh){
$ftow=$tow;
$ftoh=$ftow*($srch/$srcw);
}
else{
$ftoh=$toh;
$ftow=$ftoh*($srcw/$srch);
}
if($srcw>$tow||$srch>$toh)
{
if(function_exists("imagecreatetruecolor")){
@$ni = imagecreatetruecolor($ftow,$ftoh);
if($ni) imagecopyresampled($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
else{
$ni=imagecreate($ftow,$ftoh);
imagecopyresized($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
}
}else{
$ni=imagecreate($ftow,$ftoh);
imagecopyresized($ni,$im,0,0,0,0,$ftow,$ftoh,$srcw,$srch);
}
if(function_exists('imagejpeg')) imagejpeg($ni,$tofile);
else imagepng($ni,$tofile);
imagedestroy($ni);
}
imagedestroy($im);
}

Example Code 2

/*Constructor - generate thumbnail + watermark, parameter description:
$srcfile-image file name,
$dstfile-save file name,
$markwords-watermark text,
$markimage-watermark image,
$dstw-image saving width,
$dsth-picture saving height,
$rate-image saving quality*/
makethumb("a.jpg","b.jpg","50","50");
function makethumb($srcfile,$dstfile,$dstw,$dsth,$rate=100,$markwords=null,$markimage=null)
{
$data = getimagesize($srcfile);
switch($data[2])
{
case 1:
$im=@imagecreatefromgif($srcfile);
break;
case 2:
$im=@imagecreatefromjpeg($srcfile);
break;
case 3:
$im=@imagecreatefrompng($srcfile);
break;
}
if(!$im) return false;
$srcw=imagesx($im);
$srch=imagesy($im);
$dstx=0;
$dsty=0;
if ($srcw*$dsth>$srch*$dstw)
{
$fdsth = round($srch*$dstw/$srcw);
$dsty = floor(($dsth-$fdsth)/2);
$fdstw = $dstw;
}
else
{
$fdstw = round($srcw*$dsth/$srch);
$dstx = floor(($dstw-$fdstw)/2);
$fdsth = $dsth;
}
$ni=imagecreatetruecolor($dstw,$dsth);
$dstx=($dstx<0)?0:$dstx;
$dsty=($dstx<0)?0:$dsty;
$dstx=($dstx>($dstw/2))?floor($dstw/2):$dstx;
$dsty=($dsty>($dsth/2))?floor($dsth/s):$dsty;
$white = imagecolorallocate($ni,255,255,255);
$black = imagecolorallocate($ni,0,0,0);
imagefilledrectangle($ni,0,0,$dstw,$dsth,$white);//Fill background color
imagecopyresized($ni,$im,$dstx,$dsty,0,0,$fdstw,$fdsth,$srcw,$srch);
if($markwords!=null)
{
$markwords=iconv("gb2312","utf-8",$markwords);
//Convert text encoding
imagettftext($ni,20,30,450,560,$black,"simhei.ttf",$markwords); //Write text watermark
//The parameters are in order, text size | deflection degree | abscissa | ordinate | text color | text type | text content
}
elseif($markimage!=null)
{
$wimage_data = getimagesize($markimage);
switch($wimage_data[2])
{
case 1:
$wimage=@imagecreatefromgif($markimage);
break;
case 2:
$wimage=@imagecreatefromjpeg($markimage);
break;
case 3:
$wimage=@imagecreatefrompng($markimage);
break;
}
imagecopy($ni,$wimage,500,560,0,0,88,31); //Write image watermark, the default watermark image size is 88*31
imagedestroy($wimage);
}
imagejpeg($ni,$dstfile,$rate);
imagejpeg($ni,$srcfile,$rate);
imagedestroy($im);
imagedestroy($ni);
}
?>

Support image upload code

$pic_name=date("dmyhis");

// Generate the width of the image
$pic_width=$_post['width'];

// Generate the height of the image
$pic_height=$_post['length'];

function resizeimage($im,$maxwidth,$maxheight,$name){
//Get the current image size
$width = imagesx($im);
$height = imagesy($im);
//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;

if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
Imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
Imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
Imagejpeg ($newim,$name . ".jpg");
Imagedestroy ($newim);
}else{
Imagejpeg ($im,$name . ".jpg");
}
}

if($_files['image']['size']){
//echo $_files['image']['type'];
if($_files['image']['type'] == "image/pjpeg"||$_files['image']['type'] == "image/jpg"||$_files['image' ]['type'] == "image/jpeg"){
$im = imagecreatefromjpeg($_files['image']['tmp_name']);
}elseif($_files['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_files['image']['tmp_name']);
}elseif($_files['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_files['image']['tmp_name']);
}
if($im){
If(file_exists($pic_name.'.jpg')){
​​unlink($pic_name.'.jpg');
}
resizeimage($im,$pic_width,$pic_height,$pic_name);
Imagedestroy ($im);
}
}
?>








Generate thumbnail width:


Generate thumbnail length:



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632990.htmlTechArticleThe following provides three types of generated abbreviation codes. The compatibility is quite good, and the height and width can also be customized. function imageresize($srcfile,$tow,$toh,$tofile=) { if($tofile==){ ​​$tofile =...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template