1. 程式人生 > >php上傳圖片自動生成縮圖方法函式

php上傳圖片自動生成縮圖方法函式

$file_name='C:\AppServ\www\_MG_9888.jpg'; 
$file_new='C:\AppServ\www\bbbb.jpg';
scal_pic($file_name,$file_new);
function scal_pic($file_name,$file_new){
        //驗證引數
        if(!is_string($file_name) || !is_string($file_new)){
                return false;
        }
        //獲取圖片資訊
        $pic_scal_arr = @getimagesize($file_name);
        if(!$pic_scal_arr){
                return false;
        }
        //獲取圖象識別符號
        $pic_creat = '';
        switch($pic_scal_arr['mime']){
                case 'image/jpeg':
                        $pic_creat = @imagecreatefromjpeg($file_name);
                        break;
                case 'image/gif':
                        $pic_creat = @imagecreatefromgif($file_name);
                        break;
                case 'image/png':
                        $pic_creat = @imagecreatefrompng($file_name);
                        break;
                case 'image/wbmp':
                        $pic_creat = @imagecreatefromwbmp($file_name);
                        break;
                default:
                        return false;
                        break;
        }
        if(!$pic_creat){
                return false;
        }
        //判斷/計算壓縮大小
        $max_width = 111;//最大寬度,象素,高度不限制
        $min_width = 15;
        $min_heigth = 20;
        if($pic_scal_arr[0]<$min_width || $pic_scal_arr[1]<$min_heigth){
                return false;
        }
        $re_scal = 0;
        if($pic_scal_arr[0]>$max_width){
                $re_scal = ($max_width / $pic_scal_arr[0]);
        }
        $re_width = round($pic_scal_arr[0] * $re_scal);
        $re_height = round($pic_scal_arr[1] * $re_scal);
        //建立空圖象
        $new_pic = @imagecreatetruecolor($re_width,$re_height);
        if(!$new_pic){
                return false;
        }
        //複製圖象
        if(
[email protected]
($new_pic,$pic_creat,0,0,0,0,$re_width,$re_height,$pic_scal_arr[0],$pic_scal_arr[1])){ return false; } //輸出檔案 $out_file = ''; switch($pic_scal_arr['mime']){ case 'image/jpeg': $out_file = @imagejpeg($new_pic,$file_new); break; case 'image/jpg': $out_file = @imagejpeg($new_pic,$file_new); break; case 'image/gif': $out_file = @imagegif($new_pic,$file_new); break; case 'image/bmp': $out_file = @imagebmp($new_pic,$file_new); break; default: return false; break; } if($out_file){ return true; }else{ return false; } }

相關推薦

php圖片自動生成方法函式

$file_name='C:\AppServ\www\_MG_9888.jpg'; $file_new='C:\AppServ\www\bbbb.jpg'; scal_pic($file_name,$file_new); function scal_pic($file_n

thinkphp圖片生成

Image.php &lt;?php /** * 實現圖片上傳,圖片縮小, 增加水印 * 需要定義以下常量 * define('ERR_INVALID_IMAGE', 1); * define('ERR_NO_GD',

thinkphp圖片生成

一、上傳圖片的HTML程式碼 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> &

thinkphp5圖片生成公共方法

直接上程式碼,可以寫在公共檔案common和繼承的基礎類中,方便呼叫 /* * $name為表單上傳的name值 * $filePath為為儲存在入口資料夾public下面uploads/下面的資料夾名稱,沒有的話會自動建立 * $width指

圖片生成(ASP.NET+VB.NET)

    Sub upimages()        Dim iname As String        If Not (fileup.PostedFile Is Nothing) Then            Dim namestr1 As String = (fileu

tp5圖片生成

<?php namespace app\common\controller; use app\common\model\Goods; class Tools { public stati

PHP.25-TP框架商城應用實例-後臺1-添加商品功能、鉤子函數、在線編輯器、過濾XSS、圖片生成略圖

引用傳遞 none move 名稱 textarea 如果 library time fields 添加商品功能   1、創建商品控制器【C】  /www.test.com/shop/Admin/Controller/GoodsController.class.php

PHP圖片自動到指定解析度,保持清晰度

class AutoImage{ private $image; public function resize($src, $width, $height){ //$src 就是 $_FILES['upload_image_file']['tmp_name']

C# 檔案並生成

 #region 上傳檔案並生成縮圖 /// <summary> /// 上傳檔案並生成縮圖 /// </summary> /// <param name

Larvel5.2圖片並顯示

1.建立控制器UploadController.php <?php namespace App\Http\Controllers; use App\Http\Requests; use App\Http\Controllers\Controller; use I

使用ASP.net(C#)批量圖片自動生成,文字水印,圖片水印

因本網站上傳圖片的需要,參考很多成熟的經驗,在ASP.net平臺上使用C#語言,做了這一自動批量上傳圖片的.ASPX檔案,並經除錯成功,在本網站上使用,現發出來供大家參考,也希望高手多加指點。 本程式主要功能有: (1)可以根據自己的需要更改上傳到伺服器上的目錄,上傳

改變dede圖片路徑(商品-)去掉/allimg/ymd/

include\common.inc.phpline185://上傳的普通圖片的路徑,建議按預設//lyy $cfg_image_dir = $cfg_medias_dir.'/allimg';$cfg

PHP圖片

PHP上傳檔案,檔案的相關資訊儲存在$FILES這個全域性變數裡,檔案本身儲存在php.ini指定的臨時資料夾下。 對圖片縮放,有兩個好處,一個是安全性問題能夠滿足,第二個能夠將不必要使用大圖的地方節

php 根據url自動生成

原理:設定apache rewrite ,當圖片不存在時,呼叫php建立圖片。 例如 原圖路徑為:http://localhost/upload/news/2013/07/21/1.jpg 縮圖路徑為

PHP圖片

str func height adas post ont utf-8 mkdir dataurl HTML部分:<div class="sc-img"></div> <p><input type="file" class="f

PHP圖片例子

eight tee transform w3c 存在 false 4.3 handle ceil PHP上傳圖片例子 源碼下載 兩個文件:tu.php upload.php tu.php 代碼: 1 <?php 2 ini_set(

簡單的PHP圖片和刪除圖片示例程式碼

分享一例簡單的PHP上傳圖片和刪除圖片示例程式碼,很簡單,適合初學的朋友參考,用來研究php上傳圖片還是不錯的。 <?phpif (!empty($_FILES["img"]["name"])) { //提取檔案域內容名稱,並判斷$path=”uppic/”; //上傳路徑if(!file_exist

UEditor 圖片等比例

修改ueditor.all.js 16995行 updateTargetElement: function(){}裡的內容為 updateTargetElement: function () { var me = this; var newWidt

php生成方法封裝

------------------------------------------------- 引數: $filename : 要裁剪的圖片路徑 $destination : 要生成的圖片資料夾和路徑 $dst_w : 要把圖片裁剪到多寬 $dst_h : 要把圖片裁剪到多高

js視訊並擷取

<!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> </head> <body> <input