Uploading files to generate thumbnails

Source: Internet
Author: User
Tags imagejpeg

Yesterday the company suddenly wanted an upload file with attachments, but also a picture with the upload function and to generate thumbnail effect, I will post my code.

First look at the up.htm file, the code is as follows: <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Untitled Document </title>

<body>
<form action= "s.php" method= "post" enctype= "Multipart/form-data" Name= "Form1" Id= "Form1" >
<input type= "file" name= "image" id= "image"/>
<label>
<input type= "Submit" name= "button" id= "button" value= "submitted"/>
</label>
</form>
<label></label>
</body>
In fact, the previous page is a simple HTM file, post the information to our next file s.php

if ($_files[' image '] [' name ']) {
if ($_files[' image '] [' size ']) {
if ($_files[' image '] [' type '] = = "Image/pjpeg") {
$im = @imagecreatefromjpeg ($_files[' image '] [' tmp_name ']);
$n _bmp.= '. jpg ';
}elseif ($_files[' image '] [' type '] = = "Image/x-png") {
$im = @imagecreatefrompng ($_files[' image '] [' tmp_name ']);
$n _bmp.= '. png ';
}elseif ($_files[' image '] [' type '] = = "Image/gif") {
$im = @imagecreatefromgif ($_files[' image '] [' tmp_name ']);
$n _bmp.= '. gif ';
}
}
Resizeimage ($im, 120,60,MD5 (Date ("y-m-d h:i:s"));
Imagedestroy ($im);
$tag = 1;
}

function Resizeimage ($im, $maxwidth, $maxheight, $name) {
$width = Imagesx ($im);
$height = Imagesy ($im);
if ($maxwidth && $width > $maxwidth) | | ($maxheight && $height > $maxheight)) {
if ($maxwidth && $width > $maxwidth) {
$widthratio = $maxwidth/$width;
$RESIZEWIDTH =true;
}
if ($maxheight && $height > $maxheight) {
$heightratio = $maxheight/$height;
$RESIZEHEIGHT =true;
}
if ($RESIZEWIDTH && $RESIZEHEIGHT) {
if ($widthratio < $heightratio) {
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif ($RESIZEWIDTH) {
$ratio = $widthratio;
}elseif ($RESIZEHEIGHT) {
$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");
}
}

It is so simple, on the page this is to generate files to the directory, there is a binary on the Web page to display, of course, you can also save to the database. And then read out.

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.