㈠ php擴展之ImageMagick函數執行問題。
試一下這個類吧 phpthumb
配置phpthumb讓他使用ImageMagick來生成縮略圖
phpthumb是一個功能非常強大的縮略圖生成類,支持jpg,bmp,gif,png等格式圖片生成。無論您是PHP項目還是其它項目只要你能有一台PHP的主機就可以使用phpthumb帶給您編程上的方便。因為他還支持遠程圖片生成。
phpthumb可以配合ImageMagick來使用,這樣就不會再受到PHP內存大小的限制了,還可以生成gif動畫。下面就講如何讓phpthumb用ImageMagick來生成生成圖片。
無論win平台還是linux平台phpthumb默認是開啟ImageMagick的,只不過配置錯誤和程序有一點小問題。首先就是安裝ImageMagick這個程序。不用安裝php_imagick這個PHP擴展。phpthumb使用的是命令行操作的,所以php應該可以運行一個進程才可以,不然請選擇另外的主機。安裝好後請按下面的步驟進行phpthumb的修改。
1.打開phpThumb.config.php查找
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
// Windows: set absolute pathname
$PHPTHUMB_CONFIG['imagemagick_path'] = 'C:/Program Files/ImageMagick-6.6.0-Q16/convert.exe';
} else {
// *nix: set absolute pathname to "convert", or leave as null if "convert" is in the path (location detected with `which`)
//$PHPTHUMB_CONFIG['imagemagick_path'] = '/usr/local/bin/convert';
$PHPTHUMB_CONFIG['imagemagick_path'] = null;
}
將上面的$PHPTHUMB_CONFIG['imagemagick_path'] 設成你安裝的正確路徑。上面共計有兩個地方,第一個是window平台。第二個是LINUX,UNIX平台的。請根據您的系統選擇設定。上面紅色部分為ImageMagick路徑,替換時注意目錄分隔符是/不是\。
2.打開phpThumb.class.php
查找 function ImageMagickVersion($returnRAW=false) {在這附近
有if (eregi('^Version: [^0-9]*([ 0-9\\.\\:Q/]+) (http|file)\:', $versionstring[1], $matches)) {
替換成if (eregi('^Version: (.+) (http|file)\:', $versionstring[1], $matches)) {
OK全部設定已經完成您的phpthumb已經自動使用ImageMagick生成圖片了。
㈡ php圖片可以等比例的縮放嗎
可以。
等比例縮放的方法是:
1、載入選區--自由變換。如下圖:
2、按住shift+alt鍵,使用滑鼠調整大小,這種情況下,選區會按照等比例的方法進行縮放的。
㈢ PHP網站上傳圖片自動壓縮,怎麼編程啊,求指
這里會使用到三個文件:
connect.php:連接資料庫
test_upload.php:執行SQL語句
upload_img.php:上傳圖片並壓縮
三個文件代碼如下:
連接資料庫:connect.php
<?php
$db_host='';
$db_user='';
$db_psw='';
$db_name='';
$db_port='';
$sqlconn=newmysqli($db_host,$db_user,$db_psw,$db_name);
$q="setnamesutf8;";
$result=$sqlconn->query($q);
if(mysqli_connect_errno()){
printf("Connectfailed:%s ",mysqli_connect_error());
exit();
}
?>
當然使用一些封裝的資料庫類也是可以的。
執行SQL語句:test_upload.php
<?php
require("connect.php");
require("upload_img.php");
$real_img=$uploadfile;
$small_img=$uploadfile_resize;
$insert_sql="insertintoimg(real_img,small_img)values(?,?)";
$result=$sqlconn->prepare($insert_sql);
$result->bind_param("ss",$real_img,$small_img);
$result->execute();
?>
上傳圖片並壓縮:upload_img.php
<?php
//設置文件保存目錄
$uploaddir="upfiles/";
//設置允許上傳文件的類型
$type=array("jpg","gif","bmp","jpeg","png");
//獲取文件後綴名函數
functionfileext($filename)
{
returnsubstr(strrchr($filename,'.'),1);
}
//生成隨機文件名函數
functionrandom($length)
{
$hash='CR-';
$chars='';
$max=strlen($chars)-1;
mt_srand((double)microtime()*1000000);
for($i=0;$i<$length;$i++)
{
$hash.=$chars[mt_rand(0,$max)];
}
return$hash;
}
$a=strtolower(fileext($_FILES['filename']['name']));
//判斷文件類型
if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type))
{
$text=implode(",",$type);
$ret_code=3;//文件類型錯誤
$page_result=$text;
$retArray=array('ret_code'=>$ret_code,'page_result'=>$page_result);
$retJson=json_encode($retArray);
echo$retJson;
return;
}
//生成目標文件的文件名
else
{
$filename=explode(".",$_FILES['filename']['name']);
do
{
$filename[0]=random(10);//設置隨機數長度
$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}
while(file_exists($uploadfile));
if(move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile))
{
if(is_uploaded_file($_FILES['filename']['tmp_name']))
{
$ret_code=1;//上傳失敗
}
else
{//上傳成功
$ret_code=0;
}
}
$retArray=array('ret_code'=>$ret_code);
$retJson=json_encode($retArray);
echo$retJson;
}
//壓縮圖片
$uploaddir_resize="upfiles_resize/";
$uploadfile_resize=$uploaddir_resize.$name;
//$pic_width_max=120;
//$pic_height_max=90;
//以上與下面段注釋可以聯合使用,可以使圖片根據計算出來的比例壓縮
$file_type=$_FILES["filename"]['type'];
functionResizeImage($uploadfile,$maxwidth,$maxheight,$name)
{
//取得當前圖片大小
$width=imagesx($uploadfile);
$height=imagesy($uploadfile);
$i=0.5;
//生成縮略圖的大小
if(($width>$maxwidth)||($height>$maxheight))
{
/*
$widthratio=$maxwidth/$width;
$heightratio=$maxheight/$height;
if($widthratio<$heightratio)
{
$ratio=$widthratio;
}
else
{
$ratio=$heightratio;
}
$newwidth=$width*$ratio;
$newheight=$height*$ratio;
*/
$newwidth=$width*$i;
$newheight=$height*$i;
if(function_exists("imageresampled"))
{
$uploaddir_resize=imagecreatetruecolor($newwidth,$newheight);
imageresampled($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);
}
else
{
$uploaddir_resize=imagecreate($newwidth,$newheight);
imageresized($uploaddir_resize,$uploadfile,0,0,0,0,$newwidth,$newheight,$width,$height);
}
ImageJpeg($uploaddir_resize,$name);
ImageDestroy($uploaddir_resize);
}
else
{
ImageJpeg($uploadfile,$name);
}
}if($_FILES["filename"]['size'])
{
if($file_type=="image/pjpeg"||$file_type=="image/jpg"|$file_type=="image/jpeg")
{
//$im=imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
elseif($file_type=="image/x-png")
{
//$im=imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
elseif($file_type=="image/gif")
{
//$im=imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);
$im=imagecreatefromjpeg($uploadfile);
}
else//默認jpg
{
$im=imagecreatefromjpeg($uploadfile);
}
if($im)
{
ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);
ImageDestroy($im);
}
}
?>
請按照現實情況更改connect.php,test_upload.php中對應的信息。
望採納,謝謝。
㈣ PHP能壓縮網路上的圖片嗎
PHP圖片上傳並壓縮的實現方法具體內容如下使用到三個文件
connect.php:連接資料庫
test_upload.php:執行SQL語句
upload_img.php:上傳圖片並壓縮
三個文件代碼如下:
連接資料庫:connect.php
<?php
$db_host = '';
$db_user = '';
$db_psw = '';
$db_name = '';
$db_port = '';
$sqlconn=new mysqli($db_host,$db_user,$db_psw,$db_name);$q="set names utf8;";
$result=$sqlconn->query($q);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());exit();
}
?>
執行SQL語句:test_upload.php
<?php
require ("connect.php");
require ("upload_img.php");
$real_img=$uploadfile;
$small_img=$uploadfile_resize;
$insert_sql = "insert into img (real_img,small_img) values (?,?)";$result = $sqlconn -> prepare($insert_sql);$result -> bind_param("ss", $real_img,$small_img);$result -> execute();
?>
上傳圖片並壓縮:upload_img.php
<?php
//設置文件保存目錄
$uploaddir = "upfiles/";
//設置允許上傳文件的類型
$type=array("jpg","gif","bmp","jpeg","png");//獲取文件後綴名函數
function fileext($filename)
{
return substr(strrchr($filename, '.'), 1);}
//生成隨機文件名函數
function random($length)
{
$hash = 'CR-';
$chars = '';$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++)
{
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
$a=strtolower(fileext($_FILES['filename']['name']));//判斷文件類型
if(!in_array(strtolower(fileext($_FILES['filename']['name'])),$type)){
$text=implode(",",$type);
$ret_code=3;//文件類型錯誤
$page_result=$text;
$retArray = array('ret_code' => $ret_code,'page_result'=>$page_result);$retJson = json_encode($retArray);
echo $retJson;
return;
}
//生成目標文件的文件名
else
{
$filename=explode(".",$_FILES['filename']['name']);do
{
$filename[0]=random(10); //設置隨機數長度$name=implode(".",$filename);
//$name1=$name.".Mcncc";
$uploadfile=$uploaddir.$name;
}
while(file_exists($uploadfile));
if (move_uploaded_file($_FILES['filename']['tmp_name'],$uploadfile)){
if(is_uploaded_file($_FILES['filename']['tmp_name'])){
$ret_code=1;//上傳失敗
}
else
{//上傳成功
$ret_code=0;
}
}
$retArray = array('ret_code' => $ret_code);$retJson = json_encode($retArray);
echo $retJson;
}
//壓縮圖片
$uploaddir_resize="upfiles_resize/";
$uploadfile_resize=$uploaddir_resize.$name;//$pic_width_max=120;
//$pic_height_max=90;
//以上與下面段注釋可以聯合使用,可以使圖片根據計算出來的比例壓縮$file_type=$_FILES["filename"]['type'];
function ResizeImage($uploadfile,$maxwidth,$maxheight,$name){
//取得當前圖片大小
$width = imagesx($uploadfile);
$height = imagesy($uploadfile);
$i=0.5;
//生成縮略圖的大小
if(($width > $maxwidth) || ($height > $maxheight)){
/*
$widthratio = $maxwidth/$width;
$heightratio = $maxheight/$height;
if($widthratio < $heightratio)
{
$ratio = $widthratio;
}
else
{
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
*/
$newwidth = $width * $i;
$newheight = $height * $i;
if(function_exists("imageresampled")){
$uploaddir_resize = imagecreatetruecolor($newwidth, $newheight);imageresampled($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);}
else
{
$uploaddir_resize = imagecreate($newwidth, $newheight);imageresized($uploaddir_resize, $uploadfile, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);}
ImageJpeg ($uploaddir_resize,$name);
ImageDestroy ($uploaddir_resize);
}
else
{
ImageJpeg ($uploadfile,$name);
}
}
if($_FILES["filename"]['size'])
{
if($file_type == "image/pjpeg"||$file_type == "image/jpg"|$file_type == "image/jpeg"){
//$im = imagecreatefromjpeg($_FILES[$upload_input_name]['tmp_name']);$im = imagecreatefromjpeg($uploadfile);
}
elseif($file_type == "image/x-png")
{
//$im = imagecreatefrompng($_FILES[$upload_input_name]['tmp_name']);$im = imagecreatefromjpeg($uploadfile);
}
elseif($file_type == "image/gif")
{
//$im = imagecreatefromgif($_FILES[$upload_input_name]['tmp_name']);$im = imagecreatefromjpeg($uploadfile);
}
else//默認jpg
{
$im = imagecreatefromjpeg($uploadfile);
}
if($im)
{
ResizeImage($im,$pic_width_max,$pic_height_max,$uploadfile_resize);ImageDestroy ($im);
}
}
?>
請按照現實情況更改connect.php,test_upload.php中對應的信息
㈤ 求php圖片縮放處理函數
<?php
/**
*圖片縮放
*@paramstring$url
*@paramint$maxWidth
*@paramint$maxHeight
*@returnstring
*/
functionthumb($url,$maxWidth,$maxHeight,&$info){
$info=$imgInfo=getimagesize($url);
$width=$imgInfo[0];//獲取圖片寬度
$height=$imgInfo[1];//獲取圖片高度
$r=min($maxHeight/$height,$maxWidth/$width);
if($r>=1){//不用縮放
$maxHeight=$height;
$maxWidth=$width;
}elseif($r<1){//縮放
$maxHeight=$height*$r;
$maxWidth=$width*$r;
}
$temp_img=imagecreatetruecolor($maxWidth,$maxHeight);//創建畫布
$fun=str_replace('/','createfrom',$imgInfo['mime']);
$im=$fun($url);
imageresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);
ob_start();
$fun=str_replace('/','',$imgInfo['mime']);
$fun($temp_img);
$imgstr=ob_get_contents();
ob_end_clean();
imagedestroy($im);
return$imgstr;
}
$imgUrl=$_GET['url'];
$info=array();
$string=thumb($imgUrl,500,500,$info);
$mimeArray=explode("/",$info['mime']);
header("Content-Type:image/{$mimeArray[1]}");
echo$string;
以上代碼存為thumb.php,調用效果:
㈥ php 圖片處理 正方形好還是縮放好
在PHP網站開發過程中,如果你建立的網站涉及大量的圖片處理,必然涉及到圖片上傳,縮放,而如何保持圖片不失真,是很多初級PHP網站開發者比較頭疼的一件事,今天David就和大家分享一下如何進行圖片縮放。使用之前你需要下載安裝GD庫,以支持PHP圖片處理。下面我們結合代碼講解具體的PHP圖片縮放處理的思路。
function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)
{
$pic_width = imagesx($im);
$pic_height = imagesy($im);
if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
{
if($maxwidth && $pic_width>$maxwidth)
{
$widthratio = $maxwidth/$pic_width;
$resizewidth_tag = true;
}
if($maxheight && $pic_height>$maxheight)
{
$heightratio = $maxheight/$pic_height;
$resizeheight_tag = true;
}
if($resizewidth_tag && $resizeheight_tag)
{
if($widthratio<$heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}
if($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;
if($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;
$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;
if(function_exists("imageresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
imageresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}
else
{
$newim = imagecreate($newwidth,$newheight);
imageresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}
$name = $name.$filetype;
imagejpeg($newim,$name);
imagedestroy($newim);
}
else
{
$name = $name.$filetype;
imagejpeg($im,$name);
}
}參數說明:$im 圖片對象,應用函數之前,你需要用imagecreatefromjpeg()讀取圖片對象,如果PHP環境支持PNG,GIF,也可使用imagecreatefromgif(),imagecreatefrompng();$maxwidth 定義生成圖片的最大寬度(單位:像素)$maxheight 生成圖片的最大高度(單位:像素)$name 生成的圖片名$filetype 最終生成的圖片類型(.jpg/.png/.gif)代碼注釋:第3~4行:讀取需要縮放的圖片實際寬高第8~26行:通過計算實際圖片寬高與需要生成圖片的寬高的壓縮比例最終得出進行圖片縮放是根據寬度還是高度進行縮放,當前程序是根據寬度進行圖片縮放。如果你想根據高度進行圖片縮放,你可以將第22行的語句改成$widthratio>$heightratio第28~31行:如果實際圖片的長度或者寬度小於規定生成圖片的長度或者寬度,則要麼根據長度進行圖片縮放,要麼根據寬度進行圖片縮放。第33~34行:計算最終縮放生成的圖片長寬。第36~45行:根據計算出的最終生成圖片的長寬改變圖片大小,有兩種改變圖片大小的方法:ImageCopyResized()函數在所有GD版本中有效,但其縮放圖像的演算法比較粗糙。ImageCopyResamples(),其像素插值演算法得到的圖像邊緣比較平滑,但該函數的速度比ImageCopyResized()慢。第47~49行:最終生成經過處理後的圖片,如果你需要生成GIF或PNG,你需要將imagejpeg()函數改成imagegif()或imagepng()第51~56行:如果實際圖片的長寬小於規定生成的圖片長寬,則保持圖片原樣,同理,如果你需要生成GIF或PNG,你需要將imagejpeg()函數改成imagegif()或imagepng()。特別說明:GD庫1.6.2版以前支持GIF格式,但因GIF格式使用LZW演演算法牽涉專利權,因此在GD1.6.2版之後不支持GIF的格式。如果你是WINDOWS的環境,你只要進入PHP.INI文件找到extension=php_gd2.dll,將#去除,重啟APACHE即可,如果你是Linux環境,又想支持GIF,PNG,JPEG,你需要去下載libpng,zlib,以及freetype字體並安裝。OK,PHP圖片壓縮函數完成,最後我們概述一下整個處理的思路:通過計算實際圖片的長寬與規定生成圖片的長寬之間的縮放比例,根據實際的需求(按照寬度還是按照高度進行圖片縮放)計算出最終生成圖片的大小,然後應用PHP圖片處理函數對圖片進行處理,最後輸出圖片。以上就是關於PHP圖片處理中如何對圖片進行壓縮並保持不失真的函數說明,有疑問或者好的建議歡迎給我留言,下次我將分享在PHP網站開發建設完成後,由於圖片目錄沒有規劃好,我們該如何對圖片進行遷移的思路。
㈦ php下svg格式如何轉換為png
之前做過一個給svg圖片著色然後保存為png圖片的例子,這里分享下代碼,也是使用來實現的,可以參考下,看看你的代碼問題在哪裡。
$chinamap = '/chinamap.svg';
$im = new Imagick();
$svg = file_get_contents($chinamap );
/*著色代碼,省略*/
$im->readImageBlob($svg);
/*png settings*/
$im->setImageFormat("png24");
$im->resizeImage(720, 445, imagick::FILTER_LANCZOS, 1); /*改變大小*/
/*jpeg*/
$im->setImageFormat("jpeg");
$im->adaptiveResizeImage(720, 445); /*Optional, if you need to resize*/
$im->writeImage('/chinamap.png');/*(or .jpg)*/
$im->clear();
$im->destroy();
㈧ 如何將上傳的圖片自動縮小
PHP縮略圖生成程序
使用方法: 在支持GD庫的PHP環境中,將以下代碼另存為resize.php測試
<?
$FILENAME="image_name";
// 生成圖片的寬度
$RESIZEWIDTH=400;
// 生成圖片的高度
$RESIZEHEIGHT=400;
//生成圖片的路徑
$uploaddir="c:/winnt/temp";
function ResizeImage($im,$maxwidth,$maxheight,$name){
global $uploaddir;
$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("imageresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imageresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imageresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$uploaddir.$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$uploaddir.$name . ".jpg");
}
}
if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
}
if($im){
if(file_exists("$FILENAME.jpg")){
unlink("$FILENAME.jpg");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$FILENAME);
ImageDestroy ($im);
}
}
?>
<img src="<? echo($FILENAME.".jpg?reload=".rand(0,999999)); ?>"><br><br>
<form enctype="multipart/form-data" method="post">
<br>
<input type="file" name="image" size="50" value="瀏覽"><p>
<input type="submit" value="上傳圖片">
</form>
</body>
</html>
㈨ PHP上傳文件獲取url文件上傳上去了可是url獲取不到
你如果不記錄到資料庫,總得把文件路徑記到哪裡吧
否則,怎麼會知道文件路徑在哪裡呢
還是說你只想返迴文件路徑展示一下就罷了
那就把這一行改一下
if($db->query($sql)){
改成if(1){
就行了,恆成立
㈩ php 怎麼壓縮圖片的大小
好辦的,你把網站下載到本地,然後 用這個批量壓縮圖片的軟體就可以了