2013-03-11 137 views
-5

好吧,我有這樣的代碼,裁剪圖像,並顯示它,我想要做的是,而不是僅僅顯示它,我想將圖像保存在此文件夾中是完整的代碼如何把圖像放入目錄imagejpeg()?

<?php 
/* 
* Crop-to-fit PHP-GD 
* http://salman-w.blogspot.com/2009/04/crop-to-fit-image-using-aspphp.html 
* 
* Resize and center crop an arbitrary size image to fixed width and height 
* e.g. convert a large portrait/landscape image to a small square thumbnail 
*/ 

define('DESIRED_IMAGE_WIDTH', 512); 
define('DESIRED_IMAGE_HEIGHT', 289); 

$source_path = $_FILES['Image1']['tmp_name']; 

/* 
* Add file validation code here 
*/ 

list($source_width, $source_height, $source_type) = getimagesize($source_path); 

switch ($source_type) { 
    case IMAGETYPE_GIF: 
     $source_gdim = imagecreatefromgif($source_path); 
     break; 
    case IMAGETYPE_JPEG: 
     $source_gdim = imagecreatefromjpeg($source_path); 
     break; 
    case IMAGETYPE_PNG: 
     $source_gdim = imagecreatefrompng($source_path); 
     break; 
} 

$source_aspect_ratio = $source_width/$source_height; 
$desired_aspect_ratio = DESIRED_IMAGE_WIDTH/DESIRED_IMAGE_HEIGHT; 

if ($source_aspect_ratio > $desired_aspect_ratio) { 
    /* 
    * Triggered when source image is wider 
    */ 
    $temp_height = DESIRED_IMAGE_HEIGHT; 
    $temp_width = (int) (DESIRED_IMAGE_HEIGHT * $source_aspect_ratio); 
} else { 
    /* 
    * Triggered otherwise (i.e. source image is similar or taller) 
    */ 
    $temp_width = DESIRED_IMAGE_WIDTH; 
    $temp_height = (int) (DESIRED_IMAGE_WIDTH/$source_aspect_ratio); 
} 

/* 
* Resize the image into a temporary GD image 
*/ 

$temp_gdim = imagecreatetruecolor($temp_width, $temp_height); 
imagecopyresampled(
    $temp_gdim, 
    $source_gdim, 
    0, 0, 
    0, 0, 
    $temp_width, $temp_height, 
    $source_width, $source_height 
); 

/* 
* Copy cropped region from temporary image into the desired GD image 
*/ 

$x0 = ($temp_width - DESIRED_IMAGE_WIDTH)/2; 
$y0 = ($temp_height - DESIRED_IMAGE_HEIGHT)/2; 
$desired_gdim = imagecreatetruecolor(DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT); 
imagecopy(
    $desired_gdim, 
    $temp_gdim, 
    0, 0, 
    $x0, $y0, 
    DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT 
); 

/* 
* Render the image 
* Alternatively, you can save the image in file-system or database 
*/ 
header('Content-type: image/jpeg'); 
imagejpeg($desired_gdim); 
/* 
* Add clean-up code here 
*/ 
?> 

我的問題是在最後一行,其中imagejpeg($desired_gdim); 應該imagejpeg($desired_gdim, 'img/whatever-filename.jpg');

但圖像沒有在瀏覽器甚至表示,它不會被髮送到該目錄,另外,如果我想與我已使用相同的上傳它的文件名中插入它事情發生只是一個破碎的圖像的小圖標

+1

header(content-type:image/jpeg),你想要顯示圖片,只需使用'imagejpeg($ desired_gdim $ filename)' – 2013-03-11 08:01:14

+0

定義「不工作」。發生了什麼或沒有發生什麼?你有任何錯誤?你檢查過了嗎? – deceze 2013-03-11 08:01:18

+0

刪除標題(內容類型),它用於渲染不保存。 Regards – 2013-03-11 08:02:04

回答

0

您必須在兩者之間進行選擇 - 顯示圖像或將其保存到目錄。你不能這樣做。

imagejpeg($gd_object); //shows the image, requires content-type to be image/jpeg 

imagejpeg($gd_object, $filepath); //saves the image, to $filepath 

如果您想顯示,您可以嘗試保存圖像,然後將使用重定向到圖像。

imagejpeg($gd_object, $filepath); 
header("Location: $filepath"); 
+0

非常感謝的人 – user1950777 2013-03-16 23:25:18

0

改變你的路徑「IMG /不管-filename.jpg」到文檔根目錄就可以開始使用$ _ SERVER [「DOCUMENT_ROOT」],並附加你的願望路徑。例如$ _SERVER ['DOCUMENT_ROOT']。'/ img/whatever-filename.jpg';