2014-10-09 238 views
1

我正在將圖像上傳到我的文件夾,並且我已經從上傳的圖像創建了一個縮略圖。從現有圖像創建較小尺寸的縮略圖

/* 
save this image to try -- 
http://images.dpchallenge.com/images_challenge/0-999/319/800/Copyrighted_Image_Reuse_Prohibited_157378.jpg 
*/ 

    $source = 'H:/xampp/htdocs/myfolder/new.jpg'; 
    $destination = $_SERVER['DOCUMENT_ROOT'] . 'myfolder/New/'; 
    $quality = 60; 
    $info = getimagesize($source); 
    if ($info['mime'] == 'image/jpeg') { 
     $image = imagecreatefromjpeg($source); 
    } 
    echo $image; 
    print(imagejpeg($image, $destination, $quality)); 
    imagejpeg($image, $destination, $quality); 

但是,它也沒有創造任何東西,我alreday已經

源=>/MyFolder中/

現在,如何創建一個縮略圖上傳的圖片更小的尺寸並保存到

目的地=> MyFolder文件/新建/

或可能我的做法是錯誤的,如果是的話請告訴什麼是正確的方式。

任何幫助真的很感激。

回答

2

嘗試th是它會爲JPG格式的作品和.png圖像

<?php 
$file = 'H:/xampp/htdocs/myfolder/phool.png'; 
$pathToSave = 'H:/xampp/myfolder/New/'; 
$what = getimagesize($file); 
$file_name = basename($file); 
//print "MIME ".$what['mime']; 
switch(strtolower($what['mime'])) 
{ 
    case 'image/png': 
      $img = imagecreatefrompng($file); 
      $new = imagecreatetruecolor($what[0],$what[1]); 
      imagecopy($new,$img,0,0,0,0,$what[0],$what[1]); 
      header('Content-Type: image/png'); 
      imagepng($new,$pathToSave.$file_name,9); 
      imagedestroy($new); 
     break; 
    case 'image/jpeg': 
      $img = imagecreatefromjpeg($file); 
      $new = imagecreatetruecolor($what[0],$what[1]); 
      imagecopy($new,$img,0,0,0,0,$what[0],$what[1]); 
      header('Content-Type: image/jpeg'); 
      imagejpeg($new,$pathToSave.$file_name); 
      imagedestroy($new); 
     break; 
    case 'image/gif': 
     $img = imagecreatefromgif($file); 
     break; 
    default: die(); 
} 
?> 
0

我總是發現圖像處理的痛苦,所以我使用一個名爲Intervention的庫。

這裏是例如根據您的方案,以創建一個縮略圖:

// open an image file 
$img = Image::make('H:/xampp/htdocs/myfolder/new.jpg'); 
// now you are able to resize the instance 
$img->resize(75, 75); 
// save the image as a new file 
$img->save($destination.'new.jpg'); 
+0

你能幫助我在安裝它,我使用Drupal的 – Hitesh 2014-10-09 11:43:21

+0

對不起,我從來沒有使用Drupal的前 – 2014-10-09 11:44:57

+0

NP!告訴如何安裝它在php – Hitesh 2014-10-09 11:59:39

0

,你可以做這樣的

list($sourceWidth,$sourceHeight) = getimagesize($filepath) 
$destWidth = round($sourceWidth/2); 
$destHeight = round($sourceHeight/2); 
$sourceImage = imagecreatefromjpeg($filepath); 
$destinationImage = imagecreatefromtrue($destWidth,$destHeight); 

//Now Main function for resizing purpose 
    imagecopyresized($destinationImage,$sourceImage,0,0,0,0,$destWidth,$destHeight,$sourceWidth,$sourceHeight); 

// Now You can save Image 
imagejpeg($destImage,$pathToSave); 

試試這個

+0

什麼是'$ file'和'$ filepath' – Hitesh 2014-10-09 12:00:56

+0

這是正確的'destImage'或'destinationImage'...您的代碼令人困惑請編輯 – Hitesh 2014-10-09 12:02:00

+0

更正完成!希望這可以幫助你 – justrohu 2014-10-09 12:02:49

0

可以此功能可以幫助您:

function makeThumbnails($updir, $img, $id) 
{ 
    $thumbnail_width = 134; 
    $thumbnail_height = 189; 
    $thumb_beforeword = "thumb"; 
    $arr_image_details = getimagesize("$updir" . $id . '_' . "$img"); // pass id to thumb name 
    $original_width = $arr_image_details[0]; 
    $original_height = $arr_image_details[1]; 
    if ($original_width > $original_height) { 
     $new_width = $thumbnail_width; 
     $new_height = intval($original_height * $new_width/$original_width); 
    } else { 
     $new_height = $thumbnail_height; 
     $new_width = intval($original_width * $new_height/$original_height); 
    } 
    $dest_x = intval(($thumbnail_width - $new_width)/2); 
    $dest_y = intval(($thumbnail_height - $new_height)/2); 
    if ($arr_image_details[2] == 1) { 
     $imgt = "ImageGIF"; 
     $imgcreatefrom = "ImageCreateFromGIF"; 
    } 
    if ($arr_image_details[2] == 2) { 
     $imgt = "ImageJPEG"; 
     $imgcreatefrom = "ImageCreateFromJPEG"; 
    } 
    if ($arr_image_details[2] == 3) { 
     $imgt = "ImagePNG"; 
     $imgcreatefrom = "ImageCreateFromPNG"; 
    } 
    if ($imgt) { 
     $old_image = $imgcreatefrom("$updir" . $id . '_' . "$img"); 
     $new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height); 
     imagecopyresized($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height); 
     $imgt($new_image, "$updir" . $id . '_' . "$thumb_beforeword" . "$img"); 
    } 
} 
相關問題