2014-07-20 22 views
-2

我想要做的是將每個圖像的大小調整爲300x300。我的問題是在我目前的代碼中,一些移動到上傳文件夾的圖像文件很大。我希望上傳文件夾中的所有圖像文件的大小爲300x300。如何使用php調整圖像文件的大小

電流PHP代碼:

<?php 
include_once('../dbc/database.php'); 

$db = new Connection(); 
$db = $db->dbConnect(); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

$emailCodeResult = isset($_POST['emailCodeResult']) ? $_POST['emailCodeResult'] : ""; 

$imageLink = isset($_POST['imageLink']) ? $_POST['imageLink'] : ""; 
const path = "Oppa/upload/"; 
$s= explode(path,$imageLink); 
unlink("../upload/".$s[1]); 

$email = isset($_POST['email']) ? $_POST['email'] : ""; 

$type = $_FILES["imageInput"]["type"]; 
$ext = end(explode('/', $type)); 
$filename = uniqid() . '_' .$emailCodeResult . '.' . $ext; 
move_uploaded_file($_FILES["imageInput"]["tmp_name"], "../upload/" . $filename); 
$location = "Oppa/upload/" . $filename; 

if(!empty($_POST['email'])) { 

     $q = "UPDATE tbl_user SET user_image = '$location' WHERE user_email= :email "; 
     $query = $db->prepare($q); 
     $query->bindParam(':email', $email); 
     $results = $query->execute(); 
     echo "1"; 

} 
?> 
+0

請在發佈任何問題之前谷歌。有大量的教程解釋瞭如何使用[ImageMagick](http://php.net/manual/en/book.imagick.php)或[PHP GD](http://php.net/)調整圖像大小手冊/ zh/book.image.php)功能。 – David

回答

0

這裏是調整圖像的代碼。這可以幫助你

   $path = getcwd(); 
      $oldpic = $path.'/images/test.jpg'; //your image path 
      $array = explode("/",$oldpic); 
      $count = count($array); 
      $name = $array[$count-1]; 

      $src = $oldpic; 
      $dest = $path."/images/thumbnail/".$name; // resized image 

      //Genrating the image from there extension 
      $size = getimagesize($src); 
      switch($size["mime"]){ 

         case "image/jpeg": 
          $source_image = imagecreatefromjpeg($src); //jpeg file 
         break; 

         case "image/gif": 
          $source_image = imagecreatefromgif($src); //gif file 
         break; 

         case "image/png": 
          $source_image = imagecreatefrompng($src); //png file 
         break; 

         default: 
          $source_image=false; 
         break; 
      } 
      $width = imagesx($source_image); 
      $height = imagesy($source_image); 
      $newwidth=300; 
      $newheight= 300; 
      $virtual_image = imagecreatetruecolor($newwidth, $newheight); 
      imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
      imagejpeg($virtual_image,$dest,100);