Support file upload compatibility good picture thumbnail program _php tutorial

Source: Internet
Author: User
Tags imagejpeg
The following provides three code generation, compatibility is quite good, you can also customize the height and width oh.

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 GIF format pictures, please use JPEG or PNG format!" Return ";
Exit ();
}
$im = Imagecreatefromgif ($srcfile);
Break
Case 2:
if (!function_exists ("Imagecreatefromjpeg")) {
echo "Your GD library can not use JPEG format pictures, please use other format pictures!" 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 two

/* constructor-Generate thumbnail + watermark, parameter description:
$srcfile-picture file name,
$dstfile-Save file name,
$markwords-Watermark text,
$markimage-Watermark picture,
$DSTW-Picture Save width,
$dsth-Picture Save height,
$rate-Picture Preservation 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 a text watermark
parameter is, text size | deflection | horizontal | 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, watermark image size defaults to 88*31
Imagedestroy ($wimage);
}
Imagejpeg ($ni, $dstfile, $rate);
Imagejpeg ($ni, $srcfile, $rate);
Imagedestroy ($im);
Imagedestroy ($ni);
}
?>

Support Image upload Code

$pic _name=date ("Dmyhis");

Create the width of the picture
$pic _width=$_post[' width ';

Height of the resulting picture
$pic _height=$_post[' length '];

Function Resizeimage ($im, $maxwidth, $maxheight, $name) {
//Get the current picture 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);
}
}
?>

">


http://www.bkjia.com/PHPjc/632990.html www.bkjia.com true http://www.bkjia.com/PHPjc/632990.html techarticle The following provides three code generation, compatibility is quite good, you can also customize the height and width oh. function Imageresize ($srcfile, $tow, $toh, $tofile =) {if ($tofile = =) {$tofile = ...

  • Related Article

    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.