2013-02-16 224 views
0

我有一個按鈕,它將一個背景圖像上傳到一個文件夾並將文件名保存到數據庫,但是我無法確定如何在上傳之前重新調整圖像的大小它。其實我面臨着兩個問題。 1 - 如何調整圖像大小並上傳。 2 - 如何將圖像顯示爲具有不同尺寸的div的背景圖像。如何重新調整圖像大小並保持長寬比

我做了什麼至今:

的Html

<div class="image_load_div"> 
<form id="imageform" enctype="multipart/form-data" method="post" action="upload.php"> 
    <input name="photoimg" id="photoimg" type="file"/> 
</form> 
</div> 

的JavaScript

$("#imageform").ajaxForm().submit(); 

PHP - 上傳文件

$valid_formats = array("jpg", "png", "gif", "bmp"); 
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") { 
    name = $_FILES['photoimg']['name']; 
    $size = $_FILES['photoimg']['size']; 
    if(strlen($name)) { 
list($txt, $ext) = explode(".", $name); 
if(in_array($ext,$valid_formats)) { 
    if($size<(1024*1024)) { 
      session_start(); 
     $id = $_SESSION['QuestionId']; 
     $path = "/images/Img/".$id."_bg.".$ext; 

      if(move_uploaded_file($_FILES["photoimg"]["tmp_name"],$path)) { 
      // Save the file name into database 
      } 
     else { echo "<script>alert('Upload failed');</script>"; } 
    else { echo "<script>alert('Image file size max 1 MB');</script>"; }     
    else { echo "<script>alert('Invalid file format..');</script>"; } 
else { echo "<script>alert('Please select image..!');</script>"; exit; } 

我想將圖像保存爲height:408px;寬度:490px; 和顯示圖像時,我想顯示此圖像寬度:174px;高度:108px; IMP!在上傳時和顯示時,需要考慮縱橫比; 現在上傳正在工作。

請幫我解決這個問題。謝謝。

回答

1

這是工作和很好的測試碼。希望它能爲你工作。

調用梅索德:

$newname="xyz"; 
$filename=$_FILES['featured-img']['name']; 
$extension=strtolower(substr(strrchr($filename, '.'), 1)); //Get extension 
$extension=trim($extension); 
$newfilename=$newname.$extension; 
$newfilename=preg_replace('/\s+/', '_',$newfilename); 

$target1 = "directory_path".$newfilename; 
if(move_uploaded_file($_FILES['featured-img']['tmp_name'],$target1)); 
scaleImage($target1,500, 350, $target1); 

方法認定中:

function scaleImage($source_image_path, $maxWidth, $maxHeight, $thumbnail_image_path){ 


      list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path); 
    switch ($source_image_type) { 
     case IMAGETYPE_GIF: 
      $source_gd_image = imagecreatefromgif($source_image_path); 
      break; 
     case IMAGETYPE_JPEG: 
      $source_gd_image = imagecreatefromjpeg($source_image_path); 
      break; 
     case IMAGETYPE_PNG: 
      $source_gd_image = imagecreatefrompng($source_image_path); 
      break; 
    } 
    if ($source_gd_image === false) { 
     return false; 
    } 
    $thumbnail_image_width=$maxWidth; 
    $thumbnail_image_height=$maxHeight; 


    $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height); 
    imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height); 
    imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90); 
    imagedestroy($source_gd_image); 
    imagedestroy($thumbnail_gd_image); 
    return true; 


} 
+0

太謝謝你了:) ..就像一個魅力!!!!謝謝你。 – user1846348 2013-02-17 21:05:05

0

你可以爲此使用CSS,並且仍然保持比例。

<img src="image.png" style="max-width: 400px;"> 

那麼你可以使用PHP來使圖像更小的尺寸爲捕獲所有最大尺寸,然後用CSS來微調它的大小

相關問題