我正在尝试使用 php-image-magician 显示上传的图像以及不同大小的文本水印而不保存图像。
这是代码:
<?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 丢失或无效”我是这方面的新手。请帮我。