2011-04-17 73 views
4

正如標題所說..如何將圖像移動/重命名爲新文件夾? 我有這個迄今爲止,新的圖像的大小/裁剪,但它不會移動到「新/」文件夾:如何將圖像移動到新文件夾?

$in_filename = '4csrWqu9ngv.jpg'; 

list($width, $height) = getimagesize($in_filename); 

$offset_x = 0; 
$offset_y = 0; 

$new_height = $height - 65; 
$new_width = $width; 

$image  = imagecreatefromjpeg($in_filename); 
$new_image = imagecreatetruecolor($new_width, $new_height); 
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height); 

header('Content-Type: image/jpeg'); 
imagejpeg($new_image); 

$move_new = imagejpeg($new_image); 

rename($move_new, 'new/' . $move_new); 

一如往常的任何幫助表示讚賞:)

回答

5

你在代碼中犯了幾個錯誤。 imagejpeg的輸出是一個布爾值,所以你的重命名總是失敗。你也從來沒有保存調整大小的圖像。您必須使用imagejpeg的第二個參數並提供新圖像的正確文件名。另外,確保目錄new存在,否則重命名將失敗。

固定碼:

$in_filename = '4csrWqu9ngv.jpg'; 

list($width, $height) = getimagesize($in_filename); 

$offset_x = 0; 
$offset_y = 0; 

$new_height = $height - 65; 
$new_width = $width; 

$image = imagecreatefromjpeg($in_filename); 
$new_image = imagecreatetruecolor($new_width, $new_height); 
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height); 

/* Uncomment in case you want it also outputted 
header('Content-Type: image/jpeg'); 
imagejpeg($new_image); 
*/ 

imagejpeg($new_image, $in_filename); 

rename($in_filename, 'new/' . $in_filename); 
+0

謝謝你糾正我的錯誤,現在的偉大工程:) – Dizzi 2011-04-24 20:46:02

2

也許this應該幫助您。

0

是否存在「新」文件夾?如果沒有,您需要首先使用mkdir創建它。