2013-06-30 49 views
0

我想顯示一個上傳的圖像,隨着文本水印不同大小使用php圖像魔術師沒有保存圖像。圖像調整大小使用php圖像魔術師

這裏是代碼:

<?php 

$errors = array(); 

function mimeValidator($imagefile) 
{ 
$imageInfo = getimagesize($imagefile); 
$allowedMimes = array('image/jpg', 'image/jpeg', 'image/png'); 

if(!in_array($imageInfo['mime'], $allowedMimes)) 
$errors[] = "Only jpg and png images are supported"; 
} 

function renderImage($imageSource, $watermark, $fontSize, $font) 
{ 
$imageInfo = getimagesize($imageSource); 
list($width,$height) = getimagesize($imageSource); 

if($imageInfo['mime'] == 'image/jpg' || $imageInfo['mime'] == 'image/jpeg') 
$imageSource = imagecreatefromjpeg($imageSource); 
elseif($imageInfo['mime'] == 'image/png') 
$imageSource = imagecreatefrompng($imageSource); 

$image = imagecreatetruecolor($width, $height); 
$blue = imagecolorallocate($image, 79, 166, 185); 
$text = imagettftext($imageSource, $fontSize, 0, 20, ($height-20), $blue, $font,  $watermark); 

ob_start(); 
imagejpeg($imageSource); 
$image = ob_get_contents(); 
ob_end_clean(); 

return base64_encode($image); 
} 

?><html> 
<head> 
<title> 
image Watermark and resize 
</title> 

<style type="text/css"> 
body{ width:800px; margin: 15px auto; padding:0px; font-family: arial}  

</style> 
</head> 
<body> 
<form name="imageUpload" id="imageUpload" method="post" enctype="multipart/form-data" > 
<fieldset> 
<legend>Image text watermark</legend> 
Text: <input type="text" name="text" id="text"/><br /> 
Image: <input type="file" name="image" id="image"/><br /> 
<input type="submit" name="submit" id="createmark" value="Submit" /> 
</fieldset> 
<?php 

if(isset($_POST['submit'])) 
{ 
if($_POST['text'] == '') 
$errors[] = "Text is too short"; 

if($_FILES['image']['tmp_name'] == '') 
$errors[] = "Please upload a image"; 
else 
mimeValidator($_FILES['image']['tmp_name']); 

if(count($errors) == 0) 
       { 
include('php-image-magician/php_image_magician.php'); 

$magicianObj = new imageLib($_FILES["image"]["tmp_name"]); 
$magicianObj -> resizeImage(100, 200); 
$magicianObj -> saveImage($imagename); 
echo "<img src=\"data:image/gif;base64," . renderImage($_FILES["image"]["tmp_name"], $_POST['text'], 30, "./arial.ttf") . "\" />"; 
} 
else 
{ 
echo "<ul>"; 

foreach($errors as $error) 
echo "<li>{$error}</li>"; 

echo "</ul>"; 
} 

} 
?> 
</form> 
</body> 
</html> 

我得到一個錯誤 「的文件C:\ XAMPP \ tmp目錄\ php9E3D.tmp缺失或無效」 我在這一個newbee。請幫幫我。

回答

1

我從來沒有使用過php-image-magician,所以下面的內容很可能是虛假信息。

從我看到:

$magicianObj = new imageLib($_FILES["image"]["tmp_name"]); 

內部,imageLib電話openImage這反過來執行以下操作: 1)檢驗該文件確實存在 2)它會檢查擴展並使用正確的imagecreatefrom_ _ _功能。

這是你的問題開始的地方。當你上傳文件到你的網絡服務器時,它們被保存在一個臨時文件名下(在你的例子中,「php9E3D.tmp」)。此擴展名(.tmp)不在php-image-magician(.jpg,.jpeg,.gif,.png,.bmp,.psd)的允許擴展名列表中。因此,內部image變量被設置爲false,整個庫將不起作用。

您有幾種選擇來解決這個問題:

1)重寫PHP圖像魔法師(特別openImage),然而這並不推薦。

2)使用move_uploaded_file重命名您上傳的文件,請參見示例#2 here

3)獲得PHP-圖像魔術師擺脫你的依賴和使用PHP的內部函數(imagescale和朋友,你已經創建圖像資源在renderImage功能一起工作)

希望這有助於。

+0

良好的呼叫!可以提供有很多調整大小的PHP腳本,這些腳本寫得很好。只需做一個簡單的Google搜索。 – OptimusCrime