Reputation: 159
i am trying to display an uploaded image along with text watermark in a different size using php-image-magician without saving the image.
here are the codes :
<?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>
i am getting an error "file C:\xampp\tmp\php9E3D.tmp is missing or invalid" i am a newbee in this. please help me.
Upvotes: 0
Views: 2576
Reputation: 5857
I never used php-image-magician, so the following may very well be false information.
From what I see:
$magicianObj = new imageLib($_FILES["image"]["tmp_name"]);
Internally, imageLib
calls openImage
which in turn does the following:
1) it checks if the file actually exists
2) it checks the extension and uses the correct imagecreatefrom___ function.
This is where your problems start. When you upload files to your webserver, they get saved under a temporary filename (in your example, "php9E3D.tmp"). This extension (.tmp) is not in the list of allowed extensions for php-image-magician (.jpg, .jpeg, .gif, .png, .bmp, .psd). As such, the internal image
variable gets set to false and the whole library won't work.
You have several options to tackle this issue:
1) Rewrite php-image-magician (specifically openImage
), this however is not recommended.
2) Rename your uploaded file using move_uploaded_file
, see Example #2 here.
3) Get rid of your dependency on php-image-magician and use PHPs internal functions (imagescale
and friends, you're already creating an image resource to work with in your renderImage function)
Hope this helps.
Upvotes: 1