2009-12-22 97 views
1

按給我的任務,我想看看PHP的以下兩個功能的效果上的圖像文件 1. imagecreatefromjpeg 2. imagejpeg問題使用imagecreatefromjpeg和imagejpeg

我上傳的文件使用HTML,然後我的PHP代碼如下所示:

<?php 

try{ 
    if(!$image=imagecreatefromjpeg('zee1.jpg')){ 
     throw new Exception('Error loading image'); 
    } 
    // create text color for jpg image 
    if(!$textColor=imagecolorallocate($image,0,255,0)){ 
     throw new Exception('Error creating text color'); 
    } 
    // include text string into jpg image 
    if(!$text=imagestring($image,5,10,90,'This is a sample text 
string.',$textColor)){ 
     throw new Exception('Error creating image text'); 
    } 
    header("Content-type:image/jpeg"); 
    // display image 
    imagejpeg($image, 'zee1After.jpg'); 
    // free up memory 
    imagedestroy($image); 
} 
catch(Exception $e){ 
    echo $e->getMessage(); 
    exit(); 
} 

    ?> 

但是,當我這樣做,我得到下面的輸出:

致命錯誤:用盡33554432個字節允許內存大小(試圖在第3行的C:\ Users \ zee \ Documents \ Flex Builder 3 \ CLOUD \ bin-debug \ upload_file.php中分配10368字節)

原始圖像的大小爲:5,136 KB運行PHP。

但如果我嘗試其他的圖像大小爲:2,752 KB它工作..

可有人請幫我這。 Zeeshan

回答

4

首先刪除header("Content-type:image/jpeg");行,由於您使用的是imagejpeg()函數的文件名參數,因此它什麼都不做。

其次,以避免內存問題,您應該更改內存限制,是這樣的:

ini_set('memory_limit', -1); 

應解決您的問題(將其放置在文件的開頭)。

要恢復原來的內存限制,你可以在文件的結尾處添加以下行:

ini_restore('memory_limit'); 

整個腳本應該是這個樣子:

<?php 

ini_set('memory_limit', -1); 

try 
{ 
    if (!$image = imagecreatefromjpeg('zee1.jpg')) 
    { 
     throw new Exception('Error loading image'); 
    } 

    // create text color for jpg image 
    if (!$textColor = imagecolorallocate($image, 0, 255, 0)) 
    { 
     throw new Exception('Error creating text color'); 
    } 

    // include text string into jpg image 
    if (!$text = imagestring($image, 5, 10, 90, 'This is a sample text string.', $textColor)) 
    { 
     throw new Exception('Error creating image text'); 
    } 

    // display image 
    imagejpeg($image, 'zee1After.jpg'); 

    // free up memory 
    imagedestroy($image); 
} 

catch (Exception $e) 
{ 
    echo $e->getMessage(); 
    exit(); 
} 

ini_restore('memory_limit'); 

?> 
0

您正在請求文件的名稱,而不是文件的路徑。嘗試:

$imgname = $_FILES["file"]["tmp_name"]; 
0

Warning: Cannot modify header information - headers already sent by

確認是不是開幕PHP標籤之前的任何字符;這是獲取該錯誤消息的原因。如果您沒有看到任何字符,則可能是文件以BOM序列開頭,這是一個字符序列,UTF文件允許瞭解該文件是以UTF-8,UTF-16 LE還是UTFF編碼BE。

0

這是因爲你通過echo輸出文本。標題不能再發送,因爲它們已經發送以便發送您的文本。考慮輸出緩衝。

See my response to a similar question

編輯:此外,請參閱Steve的帖子,關於在$_FILES陣列中使用'tmp_name'索引。