當前位置:首頁 » 文件管理 » 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 11:59:53 瀏覽:921
安卓導航怎麼關閉自動升級 發布:2024-10-13 11:51:53 瀏覽:665
電腦配置120加1t什麼意思 發布:2024-10-13 11:51:10 瀏覽:412
方舟如何創建建造伺服器 發布:2024-10-13 11:21:05 瀏覽:598
win7用戶文件夾改名 發布:2024-10-13 11:16:53 瀏覽:703
小區門密碼如何重置 發布:2024-10-13 11:16:08 瀏覽:884
投訴上傳評證 發布:2024-10-13 11:03:55 瀏覽:427
pn結演算法 發布:2024-10-13 10:58:12 瀏覽:264
網路課程腳本 發布:2024-10-13 10:24:56 瀏覽:505
網上買電腦如何查看配置 發布:2024-10-13 10:17:29 瀏覽:798