1
我想爲每個項目上傳多個圖像。圖像上傳成功,但需要很長時間才能加載這些圖像。所以,我想將上傳的圖像保存爲jpeg,以便它可以輕鬆加載。保存所有圖像爲jpeg在php
我想爲每個項目上傳多個圖像。圖像上傳成功,但需要很長時間才能加載這些圖像。所以,我想將上傳的圖像保存爲jpeg,以便它可以輕鬆加載。保存所有圖像爲jpeg在php
使用PHP庫,用於圖像GD2或使用SimpleImage PHP類
$src='uploads/'.$_REQUEST['name'];
$newsrc='profiles/'.$_REQUEST['name'];
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$targ_w=$_POST['w'];
$targ_h=$_POST['h'];
$jpeg_quality = 90;
$img_r = imagecreatefromjpeg($src);//you can chose other option like imagecreatetruecolor($_POST['w'],$_POST['h']) etc
$dst_r = ImageCreateTrueColor($_POST['w'],$_POST['h']);
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);
imagejpeg($dst_r,$newsrc,$jpeg_quality) or die("Unable to save image");
imagedestroy($img_r);
}
您可以使用下面的代碼上傳圖片,將壓縮你的形象,你也可以用正常的修改將其轉換到JPG。
<?php
/*
Template Name: image compress
*/
if(isset($_POST['submit'])){
//print each file name
//echo count($_FILES['new_image']['name']);
for($i=0; $i<count($_FILES['new_image']['name']); $i++)
{
$imagename = $_FILES['new_image']['name'][$i];
$source = $_FILES['new_image']['tmp_name'][$i];
$a=$_SERVER['DOCUMENT_ROOT'];
$target = "full/path/where/you/save /image/".$imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
//$imagename = explode('.',$imagepath);
$save = "full/path/where/you/save /image/". $imagepath; //This is the new file you saving
$file = "full/path/where/you/save /image/". $imagepath; //This is the original file
list($width, $height) = getimagesize($file) ;
$tn = imagecreatetruecolor($width, $height) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $width, $height, $width, $height) ;
//.image_type_to_extension(IMAGETYPE_JPEG) image file convert
imagejpeg($tn, $save,50) ;
$thumb = imagecreatetruecolor($newwidth, $newheight);
/*$save = $a."/demo/mainn/". $imagepath; //This is the new file you saving
$file = "full/path/where/you/save /image/". $imagepath; //This is the original file
list($width, $height) = getimagesize($file) ;
$modwidth = 176;
$diff = $width/$modwidth;
$modheight = 255;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;
imagejpeg($tn, $save, 80); */
}
}
?>
<form action="<?php ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
<!--<input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />-->
<input name="new_image[]" type="file" multiple="multiple" />
<button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
</form>
把這個放在你的服務器上,而不是上傳圖像時它會壓縮20%,當它加載的時候花費的時間不多。
檢查http://php.net/manual/en/function.imagejpeg.php – 2013-05-08 04:19:13