2014-10-29 26 views
1

您好我正在尋找與我的主圖像一起創建縮略圖。我希望它被存儲在不同於原始文件夾的文件夾中,最大高度爲400像素。我搜索了一下,但似乎太複雜了。我在尋找相當簡單的東西。我是新來的文件上傳的東西。以下是我目前擁有的代碼。在不同文件夾中的PHP商店中創建縮略圖

// Configuration 
    $allowed_filetypes = array('.jpg', '.JPG', '.bmp', '.png', '.gif'); 
    $max_filesize = 100000000; 
    $upload_path = 'artimages/'; 

    $filename = $_FILES['userfile']['name']; 
    $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 

// Random Number 
    $randomnumber = rand(1, 1000000); 
    $filename = $randomnumber.$filename; 

// File Size 
    if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) 
    die('The file you attempted to upload is too large.'); 

// File Type 
    if(!in_array($ext,$allowed_filetypes)) 
    die('The file you attempted to upload is not allowed.'); 

// Check CHMOD 777 
    if(!is_writable($upload_path)) 
    die('Fail'); 

// Upload File 
    if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) 
     echo 'Success'; 
    else 
     echo 'Fail'; 

回答

1

下面的代碼需要PHP-GD

// load image and get image size 
    $img = imagecreatefromjpeg("{$pathToImages}{$fname}"); 
    $width = imagesx($img); 
    $height = imagesy($img); 

    // calculate thumbnail size 
    $new_width = $thumbWidth; 
    $new_height = floor($height * ($thumbWidth/$width)); 

    // create a new temporary image 
    $tmp_img = imagecreatetruecolor($new_width, $new_height); 

    // copy and resize old image into new image 
    imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 

    // save thumbnail into a file 
    imagejpeg($tmp_img, "{$pathToThumbs}{$fname}"); 
} 
+0

它的工作表示感謝。如果原始圖像小於所選的拇指寬度,會發生什麼情況? – Munnaz 2014-10-29 04:40:54

-1

你可以做這樣的事情:

$tmp_path = /*the temp path of your picture*/; 
$imagePath = /*the path of you uploaded picture*/; 
$Thumbnail = /*the path of your thumbnail folder*/; 

move_uploaded_file($tmp_path, $imagePath); 
copy($imagePath, $thumbnailPath); 

所以,你會上傳圖片,只是後,你將它複製在另一個地方。

+0

我需要調整縮略圖以及 – Munnaz 2014-10-29 04:21:34

+0

此答案不會調整圖像大小。 – 2014-10-29 04:34:53

+0

哦,對不起!所以Colin Schoen發佈的答案將是完美的。 – YanYan 2014-10-29 05:46:13

相關問題