php上传图片3张图片尺寸,php 上传图片并生成大中小三张缩略图_普宁汤头

本文共有两个文件

Upload.php

session_start();

require_once("../inc/resizeToFile.func.php");

$iconurl=$_FILES["userfile"]["name"];

$picture=basename($iconurl);

$arr=explode(".",$picture);

$ext="";

ini_set("display_errors",0);

?>

body,p,td{

font-size:9pt;

}

if(isset($arr[count($arr)-1]))

{

$ext=$arr[count($arr)-1];

$ext=strtolower($ext);

if($ext=="gif" or $ext=="jpg" or $ext=="jpeg")

{

//echo "你上传的文件格式是".$ext;

}else{

echo "图片格式不正确重来";

exit();

}

}else{

echo "图片格式不正确,重来";

exit();

}

if(file_exists("../images/upload/small_".$picture))

{

echo "请另选一张图片!重来";

exit();

}

if(move_uploaded_file($_FILES["userfile"]["tmp_name"],"./".$picture))

{

echo "上传成功!    ";

$_SESSION["uploaded_picture"]=$picture;

if($ext=="jpg" or $ext=="jpeg"){

$im=imagecreatefromjpeg("./".$picture);

}elseif($ext=="gif")

{

$im=imagecreatefromgif("./".$picture);

}else{

exit();

}

$width=imagesx($im);

$height=imagesy($im);

if($width>$height){

$small_width=80;

$small_height=(80/$width)*$height;

$medium_width=120;

$medium_height=(120/$width)*$height;

}else{

$small_height=80;

$small_width=(80/$height)*$width;

$medium_height=120;

$medium_width=(120/$height)*$width;

}

if($width>500 || $height>500)

{

if($width>$height)

{$large_width=500;

$large_height=(500/$width)*$height;

}else{

$large_height=500;

$large_width=(500/$height)*$width;

}

}else{

$large_width=$width;

$large_height=$height;

}

//echo $width.",".$height;

$toDir="../images/upload";

resizeToFile2($picture,$small_width,$small_height,$toDir."/small_".$picture,100);

resizeToFile2($picture,$medium_width,$medium_height,$toDir."/medium_".$picture,100);

resizeToFile2($picture,$large_width,$large_height,$toDir."/large_".$picture,100);

unlink($picture);

echo $picture."    更换图片";

}

?>

../inc/resizeToFile.func.php

/* resizeToFile resizes a picture and writes it to the harddisk

*

* $sourcefile = the filename of the picture that is going to be resized

* $dest_x       = X-Size of the target picture in pixels

* $dest_y       = Y-Size of the target picture in pixels

* $targetfile = The name under which the resized picture will be stored

* $jpegqual   = The Compression-Rate that is to be used

*/

function resizeToFile ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual)

{

/* Get the dimensions of the source picture */

$picsize=getimagesize("$sourcefile");

$source_x = $picsize[0];

$source_y = $picsize[1];

$source_id = imageCreateFromJPEG("$sourcefile");

/* Create a new image object (not neccessarily true colour) */

$target_id=imagecreatetruecolor($dest_x, $dest_y);

/* Resize the original picture and copy it into the just created image

object. Because of the lack of space I had to wrap the parameters to

several lines. I recommend putting them in one line in order keep your

code clean and readable */

$target_pic=imagecopyresampled($target_id,$source_id,

0,0,0,0,

$dest_x,$dest_y,

$source_x,$source_y);

/* Create a jpeg with the quality of "$jpegqual" out of the

image object "$target_pic".

This will be saved as $targetfile */

imagejpeg ($target_id,"$targetfile",$jpegqual);

return true;

}

function resizeToFile2 ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual)

{

$picsize=getimagesize("$sourcefile");

$source_x = $picsize[0];

$source_y = $picsize[1];

$arr=explode(".",$sourcefile);

$ext="";

if(isset($arr[count($arr)-1]))

{

$ext=$arr[count($arr)-1];

$ext=strtolower($ext);

}

if($ext=="jpg" or $ext=="jpeg"){

$source_id = imageCreateFromJPEG("$sourcefile");

}elseif($ext=="gif"){

$source_id =imagecreatefromgif("$sourcefile");

}

$target_id=imagecreatetruecolor($dest_x, $dest_y);

$target_pic=imagecopyresampled($target_id,$source_id,

0,0,0,0,

$dest_x,$dest_y,

$source_x,$source_y);

imagejpeg ($target_id,"$targetfile",$jpegqual);

return true;

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值