当前位置:首页 » 文件管理 » php上传图片生成缩略图

php上传图片生成缩略图

发布时间: 2022-08-27 11:30:37

php上传图片,如何自动生成对应的小图,保留原图。

$kf=$_FILES["pic"]['name'];
$size=$_FILES['pic']['size'];

$type=end(explode(".",$kf));//获取图片类型
$type_arr=array("gif","GIF","jpg","JPG","png","PNG","jpeg","GPEG");
if(!in_array($type,$type_arr)){
alert("很抱歉,目前只支持gif,jpg,png格式的图片!");
}
if($size>2000000){
//is_uploaded_file(
alert('上传图片小于2000KB.');
}
$filename=date("YmdHis").rand(0,100).".".$type;//新图片的完整名称
$dir="../upfile/".$userid."/";
if(!is_dir($dir)){//路径若不存在则创建
mkdir($dir);
}
//上传
if(move_uploaded_file($_FILES['pic']['tmp_name'],$dir.$filename)){
//读取文件大小
$upsize=intval($_FILES["pic"]["size"]/1024);
if($upsize>200){
$img=$dir.$filename;
upimg($img,1400,1100,$type);
}
//复制图片
if(@($dir.$filename,$dir.'s_'.$filename)){
$img=$dir.'s_'.$filename;
upimg($img,200,200,$type);
}
}

/**
* $imgval 获取文件
* $newwidth 生成缩略图宽度
* $newheight 生成缩略图高度
* $typeval 图片类型
**/
function upimg($imgval,$newwidth,$newheight,$typeval){
//extFrm($imgval."+".$newwidth."+".$newheight);
$img=$imgval;//$sdir.$filename;//获取该图片
$type=$typeval;//获取文件类型
list($width,$height)=getimagesize($img);//获取该图片大小
$newimg=imagecreatetruecolor($newwidth,$newheight);
if($type=="gif" || $type=="GIF"){
$source=imagecreatefromgif($img);
}
else if($type=="jpg" || $type=="JPG" || $type=="jpeg" || $type=="JPEG"){
$source=imagecreatefromjpeg($img);
}
else if($type=="png" || $type=="PNG"){
$source=imagecreatefrompng($img);
}
imageresampled($newimg,$source,0,0,0,0,$newwidth,$newheight,$width,$height);
if($type=="gif" || $type=="GIF"){
imagegif($newimg,$img);
}
else if($type=="jpg" || $type=="JPG" || $type=="jpeg" || $type=="JPEG"){
imagejpeg($newimg,$img);
}
else if($type=="png" || $type=="PNG"){
imagepng($newimg,$img);
}
}
主要是上传一个文件,然后复制一个出那个文件,并修改复制出来的那个文件

㈡ php上传图片生成三张缩略图,大中小。帮忙看一下

public function disposeImgAction($tmp_path, $dst_w, $file_path, $file_name)
{
$arr=getimagesize($tmp_path);
$src_w=$arr[0];
$src_h=$arr[1];
$type=$arr[2];
switch($type){
case 1:$src_im=imagecreatefromgif($tmp_path);break;
case 2:$src_im=imagecreatefromjpeg($tmp_path);break;
case 3:$src_im=imagecreatefrompng($tmp_path);break;
default:UtlsSvc::showMsg('不支持该图片类型','/coinproct/index/');
}
if ($dst_w == 500) {
$dst_h = 385;
} elseif ($dst_w == 300) {
$dst_h = 225;
} elseif ($dst_w == 100) {
$dst_h = 75;
}
$dst_im=imagecreatetruecolor($dst_w,$dst_h);
imageresized($dst_im,$src_im,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
imagejpeg($dst_im, $file_path.'/'.$file_name);
}

这是我自己写的一个函数 第一个是上传的文件临时目录 第二个是 缩略后的宽度 里面你可以自己计算高度的比例 第三个是存到什么目录 第四个是文件名

㈢ php怎么给psd图片生成缩略图

psd是不会通过缩略图显示的,用其他能打开它的软件可以外,其他都是没法的。你这个php生成缩略图是在什么地方生成嘛,在网页上面的话浏览器只支持jpg/gif/png这几种图片格式,其他的都是个别支持不通用的。
求采纳

㈣ PHP 图片上传生成缩略图

//2014年3月5日15:08:02因为需要做缩略图,所以改用thinkphp来做上传,它支持时间戳命名,方便命名,以及更名
//这是以前网络到的,然后使用的缩略图代码,需要cg库支持

/**
*生成缩略图
*@[email protected]
*@paramstring源图绝对完整地址{带文件名及后缀名}
*@paramstring目标图绝对完整地址{带文件名及后缀名}
*@paramint缩略图宽{0:此时目标高度不能为0,目标宽度为源图宽*(目标高度/源图高)}
*@paramint缩略图高{0:此时目标宽度不能为0,目标高度为源图高*(目标宽度/源图宽)}
*@paramint是否裁切{宽,高必须非0}
*@paramint/float缩放{0:不缩放,0<this<1:缩放到相应比例(此时宽高限制和裁切均失效)}
*@returnboolean
*/
functionfileext($file)
{
returnstrtolower(pathinfo($file,PATHINFO_EXTENSION));
}

functionimg2thumb($src_img,$dst_img,$width=75,$height=75,$cut=0,$proportion=0)
{
if(!is_file($src_img))
{
returnfalse;
}
$ot=$this->fileext($dst_img);
$otfunc='image'.($ot=='jpg'?'jpeg':$ot);
$srcinfo=getimagesize($src_img);
$src_w=$srcinfo[0];
$src_h=$srcinfo[1];
$type=strtolower(substr(image_type_to_extension($srcinfo[2]),1));
$createfun='imagecreatefrom'.($type=='jpg'?'jpeg':$type);

$dst_h=$height;
$dst_w=$width;
$x=$y=0;

/**
*缩略图不超过源图尺寸(前提是宽或高只有一个)
*/
if(($width>$src_w&&$height>$src_h)||($height>$src_h&&$width==0)||($width>$src_w&&$height==0))
{
$proportion=1;
}
if($width>$src_w)
{
$dst_w=$width=$src_w;
}
if($height>$src_h)
{
$dst_h=$height=$src_h;
}

if(!$width&&!$height&&!$proportion)
{
returnfalse;
}
if(!$proportion)
{
if($cut==0)
{
if($dst_w&&$dst_h)
{
if($dst_w/$src_w>$dst_h/$src_h)
{
$dst_w=$src_w*($dst_h/$src_h);
$x=0-($dst_w-$width)/2;
}
else
{
$dst_h=$src_h*($dst_w/$src_w);
$y=0-($dst_h-$height)/2;
}
}
elseif($dst_wxor$dst_h)
{
if($dst_w&&!$dst_h)//有宽无高
{
$propor=$dst_w/$src_w;
$height=$dst_h=$src_h*$propor;
}
elseif(!$dst_w&&$dst_h)//有高无宽
{
$propor=$dst_h/$src_h;
$width=$dst_w=$src_w*$propor;
}
}
}
else
{
if(!$dst_h)//裁剪时无高
{
$height=$dst_h=$dst_w;
}
if(!$dst_w)//裁剪时无宽
{
$width=$dst_w=$dst_h;
}
$propor=min(max($dst_w/$src_w,$dst_h/$src_h),1);
$dst_w=(int)round($src_w*$propor);
$dst_h=(int)round($src_h*$propor);
$x=($width-$dst_w)/2;
$y=($height-$dst_h)/2;
}
}
else
{
$proportion=min($proportion,1);
$height=$dst_h=$src_h*$proportion;
$width=$dst_w=$src_w*$proportion;
}

$src=$createfun($src_img);
$dst=imagecreatetruecolor($width?$width:$dst_w,$height?$height:$dst_h);
$white=imagecolorallocate($dst,255,255,255);
imagefill($dst,0,0,$white);

if(function_exists('imageresampled'))
{
imageresampled($dst,$src,$x,$y,0,0,$dst_w,$dst_h,$src_w,$src_h);
}
else
{
imageresized($dst,$src,$x,$y,0,0,$dst_w,$dst_h,$src_w,$src_h);
}
$otfunc($dst,$dst_img);
imagedestroy($dst);
imagedestroy($src);
returntrue;
}

㈤ PHP中如何利用jquery,ajaxupload上传图片生成缩略图

在上传脚本里面写一个函数,生成一张即可

㈥ php一次上传多张图片并生成相应的缩略图的代码

这里压缩为61x61

<?php
/*
作者:遥远的期待
QQ:15624575
个人主页:www.phptogether.com www.d1php.info
*/

function upload_multi($path,$photo,$i){
$uploaddir = './'.$path;//文件存放目录
if(!file_exists($uploaddir))//如果目录不存在就新建
$uploaddir=mkdir($uploaddir);

$piece = explode('.',$photo['name'][$i]);
$uploadfile = $uploaddir . '/'.md5($piece[0]).'.'.$piece[1];
$result = move_uploaded_file($photo['tmp_name'][$i], $uploadfile);
if(!$result){
exit('上传失败');
}
list($width_orig, $height_orig) = getimagesize($uploadfile);
if ($width_orig!=61||$height_orig!=61) {
$image_p = imagecreatetruecolor(61, 61);
if($piece[1]=="jpg"||$piece[1]=="jpeg"){
$image = imagecreatefromjpeg($uploadfile);
imageresampled($image_p, $image, 0, 0, 0, 0, 61, 61, $width_orig, $height_orig);
imagejpeg($image_p,$uploadfile);
}else if($piece[1]=="gif"){
$image = imagecreatefromgif($uploadfile);
imageresampled($image_p, $image, 0, 0, 0, 0, 61, 61, $width_orig, $height_orig);
imagegif($image_p,$uploadfile);
}
}
return basename($uploadfile);
}

if($_POST['tijiao']){
extract($_POST);
$i=0;
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
upload_multi($email,$_FILES["pictures"],$i);
}
$i++;
}
}
?>
<script language="javascript">
function go_up(){
document.getElementById('new_up').innerHTML+='<input type="file" name="pictures[]" /><br>';
}
</script>
<form action="php.php" method="post" enctype="multipart/form-data">
<p>多图片上传<br>
<input type="text" name="username" /><br>
<input type="text" name="email" /><br>
<input type="file" name="pictures[]" /><br>
<div id="new_up"></div>
<input type="button"" name="add_img" value="新增上传" onclick="go_up()"/><br>
<input type="submit" value="Send" name="tijiao"/><br>
</p>
</form>

㈦ 能直接用的PHP生成缩略图的程序(要求简单)

<?php
/*构造函数-生成缩略图+水印,参数说明:
$srcFile-图片文件名,
$dstFile-另存文件名,
$markwords-水印文字,
$markimage-水印图片,
$dstW-图片保存宽度,
$dstH-图片保存高度,
$rate-图片保存品质*/
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);// 填充背景色
ImageCopyResized($ni,$im,$dstX,$dstY,0,0,$fdstW,$fdstH,$srcW,$srcH);
if($markwords!=null)
{
$markwords=iconv("gb2312","UTF-8",$markwords);
//转换文字编码
ImageTTFText($ni,20,30,450,560,$black,"simhei.ttf",$markwords); //写入文字水印
//参数依次为,文字大小|偏转度|横坐标|纵坐标|文字颜色|文字类型|文字内容
}
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;
}
image($ni,$wimage,500,560,0,0,88,31); //写入图片水印,水印图片大小默认为88*31
imagedestroy($wimage);
}
ImageJpeg($ni,$dstFile,$rate);
ImageJpeg($ni,$srcFile,$rate);
imagedestroy($im);
imagedestroy($ni);
}
?>

㈧ php SWFUpload 怎么创建缩略图并且保存到指定文件夹里面

php /* * swfupload图片上传 */ if (isset($_POST["PHPSESSID"])) { session_id($_POST["PHPSESSID"]); } session_start(); ini_set("html_errors", "0"); if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) ||$_FILES["Filedata"]["error"] != 0) { echo "错误:无效的上传!"; exit(0); } // Get the image and create a thumbnail $file_types=explode(".",$_FILES["Filedata"]["name"]); $file_type=$file_types[count($file_types)-1]; if(strtolower($file_type)=='gif' ) { $img = imagecreatefromgif($_FILES["Filedata"]["tmp_name"]); } else if(strtolower($file_type)=='png') { $img = imagecreatefrompng($_FILES["Filedata"]["tmp_name"]); } else if(strtolower($file_type)=='bmp') { $img = imagecreatefromwbmp($_FILES["Filedata"]["tmp_name"]); } else { $img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]); } if (!$img) { echo "错误:无法创建图像 ". $_FILES["Filedata"]["tmp_name"]; exit(0); } $width = imageSX($img); $height = imageSY($img); if (!$width || !$height) { echo "错误:无效的高或高"; exit(0); } // Build the thumbnail $target_width = 100; $target_height = 100; $target_ratio = $target_width / $target_height; $img_ratio = $width / $height; if ($target_ratio > $img_ratio) { $new_height = $target_height; $new_width = $img_ratio * $target_height; } else { $new_height = $target_width / $img_ratio; $new_width = $target_width; } if ($new_height > $target_height) { $new_height = $target_height; } if ($new_width > $target_width) { $new_height = $target_width; } $new_img = ImageCreateTrueColor(100, 100); if (!@imagefilledrectangle($new_img, 0, 0, $target_width-1, $target_height-1, 0)) { // Fill the image black echo "错误:不能填充新图片"; exit(0); } if (!@imageresampled($new_img, $img, ($target_width-$new_width)/2, ($target_height-$new_height)/2, 0,0, $new_width, $new_height, $width, $height)) { echo "错误:不能调整大小的图像"; exit(0); } if (!isset($_SESSION["file_info"])) { $_SESSION["file_info"] = array(); } ob_start(); imagejpeg($new_img); $imagevariable = ob_get_contents(); ob_end_clean(); $file_id = md5($_FILES["Filedata"]["tmp_name"] + rand()*100000); $_SESSION["file_info"][$file_id] = $imagevariable; echo "FILEID:" . $file_id; // Return the file id to the script include("upimg.class.php"); if(!empty($_FILES["Filedata"]) and count(explode(",",$_SESSION["upload_tem"]))<5) { $folder="upload/images/tem/".date("Y-m-d"); $up = new upimg("$folder","$folder"); //可以写成:$up = new upimg(); $up->autoThumb = TRUE; //可省略 $up->srcDel=TRUE; $up->thumbWidth = 550; //可省略 $up->thumbHeight = 400; //可省略 $up->maxsize=2014; //上传文件大小单位是kb $result= $up->upload('Filedata'); // HTML中<input />的name属性值 $_SESSION["upload_tem"]=$_SESSION["upload_tem"].",".$up->thumbPath; $_SESSION["upload_tem"]=trim($_SESSION["upload_tem"],","); } ?>2. [代码][PHP]代码 生成缩略图类upimg.class.php: <?php class upimg{ public $uploadFolder = 'upload'; // 图片存放目录 public $thumbFolder = 'upload/thumb'; // 缩略图存放目录 public $thumbWidth = ''; // 缩略图宽度 public $thumbHeight = ''; // 缩略图高度 public $autoThumb = ''; // 是否自动生成缩略图 public $error = ''; // 错误信息 public $imgPath = ''; // 上传成功后的图片位置 public $thumbPath = ''; // 上传成功后的缩略图位置 public $maxsize=''; // 说明:初始化,创建存放目录 function __construct($uploadFolder = 'upload', $thumbFolder = 'upload/thumb'){ $this->uploadFolder = $uploadFolder; $this->thumbFolder = $thumbFolder; $this->_mkdir(); } // 说明:上传图片,参数是<input />的name属性值;成功返回图片的相对URL,失败返回FALSE和错误信息(在$this->error里) // bool/sting upload(string $html_tags_input_attrib_name); function upload($inputName){ // 上传操作,参数是input标签的name属性。 if ($this->error){ // 如果有错,直接返回(例如_mkdir) return FALSE; } if(!$_FILES[$inputName]["name"]){ $this->error = '没有上传图片'; return FALSE; } //检测文件大小 if($_FILES[$inputName]["size"] > ($this->maxsize*1024)){ $this->error = '上传文件'.$inputName.'太大,最大支持'.ceil($this->maxsize/1024).'kb的文件'; return FALSE; } if($_FILES[$inputName]["name"]){ $isUpFile = $_FILES[$inputName]['tmp_name']; if (is_uploaded_file($isUpFile)){ $imgInfo = $this->_getinfo($isUpFile); if (FALSE == $imgInfo){ return FALSE; } $extName = $imgInfo['type']; $microSenond = floor(microtime()*10000);// 取一个毫秒级数字,4位。 $newFileName = $uploadFolder . '/' . date('YmdHis') . $microSenond . '.' . $extName ; // 所上传图片的新名字。 $location = $this->uploadFolder . $newFileName; $result = move_uploaded_file($isUpFile, $location); if ($result) { if (TRUE == $this->autoThumb) { // 是否生成缩略图 $thumb = $this->thumb($location, $this->thumbWidth, $this->thumbHeight); if (FALSE == $thumb) { return FALSE; } } //是否删除原图 if(TRUE==$this->srcDel) { @unlink ($location); } $this->imgPath = $location; return $location; }else{ $this->error = '移动临时文件时出错'; return FALSE; } }else{ $uploadError = $_FILES[$inputName]['error']; if (1 == $uploadError){ // 文件大小超过了php.ini中的upload_max_filesize $this->error = '文件太大,服务器拒绝接收大于' . ini_get('upload_max_filesize') . '的文件'; return FALSE; }elseif (3 == $uploadError){ // 上传了部分文件 $this->error = '上传中断,请重试'; return FALSE; }elseif (4 == $uploadError){ $this->error = '没有文件被上传'; return FALSE; }elseif (6 == $uploadError){ $this->error = '找不到临时文件夹,请联系您的服务器管理员'; return FALSE; }elseif (7 == $uploadError){ $this->error = '文件写入失败,请联系您的服务器管理员'; return FALSE; }else{ if (0 != $uploadError){ $this->error = '未知上传错误,请联系您的服务器管理员'; return FALSE; } } // end if $uploadError } // end if is_uploaded_file else } // end if $_FILES[$inputName]["name"] } // 说明:获取图片信息,参数是上传后的临时文件,成功返回数组,失败返回FALSE和错误信息 // array/bool _getinfo(string $upload_tmp_file) private function _getinfo($img){ if (!file_exists($img)){ $this->error = '找不到图片,无法获取其信息'; return FALSE; } $tempFile = @fopen($img, "rb"); $bin = @fread($tempFile, 2); //只读2字节 @fclose($tempFile); $strInfo = @unpack("C2chars", $bin); $typeCode = intval($strInfo['chars1'] . $strInfo['chars2']); $fileType = ''; switch ($typeCode){ // 6677:bmp 255216:jpg 7173:gif 13780:png 7790:exe 8297:rar 8075:zip tar:109121 7z:55122 gz 31139 case '255216': $fileType = 'jpg'; break; case '6677': $fileType = 'bmp'; break; case '7173': $fileType = 'gif'; break; case '13780': $fileType = 'png'; break; default: $fileType = 'unknown'; } if ($fileType == 'jpg' || $fileType == 'gif' || $fileType == 'png' || $fileType == 'bmp'){ $imageInfo = getimagesize($img); $imgInfo['size'] = $imageInfo['bits']; $imgInfo["type"] = $fileType; $imgInfo["width"] = $imageInfo[0]; $imgInfo["height"] = $imageInfo[1]; return $imgInfo; }else{ // 非图片类文件信息 $this->error = '图片类型错误'; return FALSE; } } // end _getinfo // 说明:生成缩略图,等比例缩放或拉伸 // bool/string thumb(string $uploaded_file, int $thumbWidth, int $thumbHeight, string $thumbTail); function thumb($img, $thumbWidth = 300, $thumbHeight = 200,$thumbTail = '_thumb') { $filename = $img; // 保留一个名字供新的缩略图名字使用 $imgInfo = $this->_getinfo($img,$i); if(FALSE == $imgInfo) { return FALSE; } $imgType = $imgInfo['type']; switch ($imgType) { // 创建一个图,并给出扩展名 case "jpg" : $img = imagecreatefromjpeg($img); $extName = 'jpg'; break; case 'gif' : $img = imagecreatefromgif($img); $extName = 'gif'; break; case 'bmp' : $img = imagecreatefromgif($img); $extName = 'bmp'; break; case 'png' : $img = imagecreatefrompng($img); $extName = 'png'; break; default : // 如果类型错误,生成一张空白图 $img = imagecreate($thumbWidth,$thumbHeight); imagecolorallocate($img,0x00,0x00,0x00); $extName = 'jpg'; } // 缩放后的图片尺寸(小则拉伸,大就缩放) $imgWidth = $imgInfo['width']; $imgHeight = $imgInfo['height']; if($imgHeight > $imgWidth) { // 竖图 $newHeight = $thumbHeight; $newWidth = ceil($imgWidth / ($imgHeight / $thumbHeight )); } else if($imgHeight < $imgWidth) { // 横图 $newHeight = ceil($imgHeight / ($imgWidth / $thumbWidth )); $newWidth = $thumbWidth; } else if($imgHeight == $imgWidth) { // 等比例图 $newHeight = $thumbWidth; $newWidth = $thumbWidth; } $bgimg = imagecreatetruecolor($newWidth,$newHeight); $bg = imagecolorallocate($bgimg,0x00,0x00,0x00); imagefill($bgimg,0,0,$bg); $sampled = imageresampled($bgimg,$img,0,0,0,0,$newWidth,$newHeight,$imgWidth,$imgHeight); if(!$sampled ) { $this->error = '缩略图生成失败'; $this->path=$this->uploadFolder . '/' . $filename; return FALSE; } $filename = basename($filename); $newFileName = substr($filename, 0, strrpos($filename, ".")) . $thumbTail . '.' . $extName ; // 新名字 $thumbPath = $this->thumbFolder . '/' . $newFileName; switch ($extName){ case 'jpg': $result = imagejpeg($bgimg, $thumbPath); break; case 'gif': $result = imagegif($bgimg, $thumbPath); break; case 'png': $result = imagepng($bgimg, $thumbPath); break; default: // 上边判断类型出错时会创建一张空白图,并给出扩展名为jpg $result = imagejpeg($bgimg, $thumbPath); } if ($result) { $this->thumbPath = $thumbPath; $this->path=$this->uploadFolder . '/' . $filename; return $thumbPath; } else { $this->error = '缩略图创建失败'; $this->path=$this->uploadFolder . '/' . $filename; return FALSE; } } // end thumb // 说明:创建图片的存放目录 private function _mkdir() { // 创建图片上传目录和缩略图目录 if(!is_dir($this->uploadFolder)) { $dir = explode('/', $this->uploadFolder); foreach($dir as $v) { if($v) { $d .= $v . '/'; if(!is_dir($d)) { $state = mkdir($d); if(!$state) { $this->error = '在创建目录' . $d . '时出错!'; } } } } } if(!is_dir($this->thumbFolder) && TRUE == $this->autoThumb) { $dir = explode('/', $this->thumbFolder); foreach($dir as $v) { if($v) { $d .= $v . '/'; if(!is_dir($d)) { $state = mkdir($d); if(!$state) { $this->error = '在创建目录' . $d . '时出错!'; } } } } } } } ?>

㈨ 客户端向php服务器端提交图片并生成缩略图

你的问题,我有点懵了,现在是在本地没问题,到服务器有问题,还是程序本身有问题,如果程序问题,把程序贴出来

㈩ 100分求,谁能帮我写个PHP上传图片并自动生成缩略图,再获取外链这样的程序

给你生成缩略图部分的代码吧,,相信你其他的部分肯定能自己搞定的

//所略图,等比例缩放
function makemicroimage($img_s, $imgDir, $imgs_h, $imgs_w, $quality = 100) {
$imgs_hs = $imgs_h;
$imgs_ws = $imgs_w;
$imginfo_s = getimagesize($img_s);

$newh = $imginfo_s[0];//原图高
$neww = $imginfo_s[1];//原图宽

if (($neww > $imgs_w) || ($newh > $imgs_h)) {
if($neww > $imgs_w) {
$neww = $imgs_w;
$newh = ($imgs_w / $imginfo_s[1]) * $imginfo_s[0];
}else{
$newh = $imgs_h;
$neww = ($imgs_h / $imginfo_s[0]) * $imginfo_s[1];
}
}
$imgs_h = intval($newh);
$imgs_w = intval($neww);

if(($imgs_w > $imgs_ws) || ($imgs_h > $imgs_hs)){

if($imgs_w > $imgs_ws) {
$imgs_w = $imgs_ws;
$imgs_h = ($imgs_ws/$imginfo_s[1]) * $imginfo_s[0];
}else{
$imgs_h = $imgs_hs;
$imgs_w = ($imgs_hs / $imginfo_s[0]) * $imginfo_s[1];
}
$imgs_h = intval($imgs_h);
$imgs_w = intval($imgs_w);

}
$imginfo_s[2];

if($imginfo_s[2] == 1) //处理的图片为gif格式。
{
$imgfrom = imagecreatefromgif($img_s);
if($imgfrom) {
$imgto = imagecreatetruecolor($imgs_h, $imgs_w);
//$info=imageresized($imgto,$imgfrom,0,0,0,0,$imgs_h,$imgs_w,$imginfo_s[0],$imginfo_s[1]);
$info = imageresampled($imgto, $imgfrom, 0, 0, 0, 0, $imgs_h, $imgs_w, $imginfo_s[0], $imginfo_s[1]);
imagegif($imgto, $imgDir, $quality);
imagedestroy($imgto);
}else {
return 0;
}
}else if($imginfo_s[2] == 2) //处理的图片格式为jpg或者jpeg
{
$imgfrom = imagecreatefromjpeg($img_s);
if($imgfrom) {
$imgto = imagecreatetruecolor($imgs_h, $imgs_w);
//$info=imageresized($imgto,$imgfrom,0,0,0,0,$imgs_h,$imgs_w,$imginfo_s[0],$imginfo_s[1]);
$info = imageresampled($imgto, $imgfrom, 0, 0, 0, 0, $imgs_h, $imgs_w, $imginfo_s[0], $imginfo_s[1]);
imagejpeg($imgto, $imgDir, $quality);
imagedestroy($imgto);
}else {
return 0;
}
}else if($imginfo_s[2] == 3) //处理的图片格式为png
{
$imgfrom = imagecreatefrompng($img_s);
if($imgfrom) {
$imgto = imagecreatetruecolor($imgs_h, $imgs_w);
//$info=imageresized($imgto,$imgfrom,0,0,0,0,$imgs_h,$imgs_w,$imginfo_s[0],$imginfo_s[1]);
$info = imageresampled($imgto, $imgfrom, 0, 0, 0, 0, $imgs_h, $imgs_w, $imginfo_s[0], $imginfo_s[1]);
imagepng($imgto, $imgDir, $quality);
imagedestroy($imgto);
}else {
return 0;
}
}
}

参数分别为图片源地址和生成的图片地址 图片尺寸。。

热点内容
网上买电脑如何查看配置 发布:2024-10-13 10:17:29 浏览:794
遗传算法非线性约束 发布:2024-10-13 10:09:16 浏览:779
图像扭曲的算法 发布:2024-10-13 09:56:11 浏览:234
c语言的精髓 发布:2024-10-13 09:56:09 浏览:814
嵌入式系统高级c语言编程 发布:2024-10-13 09:16:26 浏览:87
天刀与服务器断开是什么鬼 发布:2024-10-13 09:12:12 浏览:72
python金融量化 发布:2024-10-13 09:12:11 浏览:84
搭建hive需要什么服务器 发布:2024-10-13 09:07:16 浏览:399
c静态成员函数的访问 发布:2024-10-13 09:03:08 浏览:529
服务器怎么固定ip 发布:2024-10-13 09:03:08 浏览:905