[英]get <img> tag from @imagecreatefromjpeg?
I am writing text over image using PHP.I am easily creating new image from current image and writing text over it using PHP.But I also want that when I click on Apply Changes then move this new image to my directory (move_uploaded_file) and replace the current image with the new image and new image name must be the same of the previous image because I am downloading it using PHP. 我正在使用PHP在图像上写文本。我很容易从当前图像创建新图像并在PHP上写文本。但是我也想当我单击Apply Changes然后将新图像移动到我的目录(move_uploaded_file)并替换具有新图像和新图像名称的当前图像必须与先前图像相同,因为我正在使用PHP下载它。
Here is my code which I am using to wrote text over it. 这是我用来在其上编写文本的代码。
HTML code : HTML代码:
<img id="image" src="<?php echo "upload_pic/" . $_FILES["image_file"]["name"]; ?>" alt="your_image" />
<input type="button" name="save_image" id="save_image" value="Save Image" />
<input type="hidden" id="hidden_image_name" name="hidden_image_name" value="<?php echo $_FILES["image_file"]["name"]; ?>" />
jQuery Code : jQuery代码:
jQuery('#save_image').click(function(){
var image_name = jQuery('#hidden_image_name').val();
jQuery.ajax({
url:'text_image.php',
data:'file='+image_name,
type:'get',
success:function(data){
alert(data);
}
});
});
text_image.php text_image.php
<?php
$file = 'upload_pic/'.$_GET['file'];
/*** set the header for the image ***/
header("Content-type: image/jpeg");
/*** specify an image and text ***/
$im = writeToImage($file, 'PHPRO rules again');
//echo $im;
/*** spit the image out the other end ***/
imagejpeg($im);
/**
*
* @Write text to an existing image
*
* @Author Kevin Waterson
*
* @access public
*
* @param string The image path
*
* @param string The text string
*
* @return resource
*
*/
function writeToImage($imagefile, $text){
/*** make sure the file exists ***/
if(file_exists($imagefile))
{
/*** create image ***/
$im = @imagecreatefromjpeg($imagefile);
/*** create the text color ***/
$text_color = imagecolorallocate($im, 233, 14, 91);
/*** splatter the image with text ***/
imagestring($im, 6, 25, 150, "$text", $text_color);
}
else
{
/*** if the file does not exist we will create our own image ***/
/*** Create a black image ***/
$im = imagecreatetruecolor(150, 30); /* Create a black image */
/*** the background color ***/
$bgc = imagecolorallocate($im, 255, 255, 255);
/*** the text color ***/
$tc = imagecolorallocate($im, 0, 0, 0);
/*** a little rectangle ***/
imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
/*** output and error message ***/
imagestring($im, 1, 5, 5, "Error loading $imagefile", $tc);
}
return $im;
}
?>
Thanks in Advanced! 提前致谢!
You want to reload the IMG source upon change. 您想在更改后重新加载IMG源。 So what you need to do is to replace the SRC=
value of the IMG tag, by adding a dummy query to avoid caching: 因此,您需要做的是通过添加虚拟查询来避免缓存来替换IMG标签的SRC=
值:
jQuery('#save_image').click(function(){
var image_name = jQuery('#hidden_image_name').val();
jQuery.ajax({
url:'text_image.php',
data:'file='+image_name,
type:'get',
success:function(data){
jQuery('#image').attr('src', jQuery('#image')
.attr('src')+'?'+Math.random());
}
});
});
The PHP script needs output nothing, just rewrite the image file PHP脚本不需要输出任何内容,只需重写图像文件
<?php
$file = 'upload_pic/'.$_GET['file'];
$im = writeToImage($file, 'PHPRO rules again');
imageJPEG($im, $file, 95); // 95% quality
die();
...
?>
You may want to output a JSON stating 'success' or 'failure', though, just in case. 不过,为了以防万一,您可能需要输出表示“成功”或“失败”的JSON。
You also want to check that $_GET['file']
is a valid, accessible, allowed, image. 您还想检查$_GET['file']
是有效的,可访问的,允许的图像。 For example you might force the file to be a basename: 例如,您可以强制文件为基名:
$file = 'upload_pic/'.basename($_GET['file']);
if (!file_exists($file))
// ...no such file...
if (false === getImageSize($file))
// ...doesn't look like an image...
// etc.
If you want to keep both original and 'captioned' image, you need to create a new file , and therefore output the new name so that jQuery can replace the SRC attribute. 如果要保留原始图像和“带字幕”图像,则需要创建一个新文件 ,并输出新名称,以便jQuery可以替换SRC属性。
For example: 例如:
$new = 'upload_pic/'.uniqid().'.jpg';
...create the file and put $im into it, as above...
Header('Content-Type: application/json;charset=utf8');
die(json_serialize(array("src" => $new)));
// Or if you don't want json_serialize (BUT THEN $new may only contain printable ASCII-7 characters that need no escaping. "[a-z][0-9]-_." if you want to be sure).
die("{\"src\":\"{$new}\"}");
in jQuery, data
will now contain $new
, so: 在jQuery中, data
现在将包含$new
,因此:
success:function(data){
jQuery('#image').attr('src', data.src);
}
(in the page, you may need to update other fields if you want to re-caption an already-captioned image). (在页面中,如果要重新为已字幕的图像添加字幕,则可能需要更新其他字段)。
I would send a XMLHttpRequest (AJAX) with jQuery, because window.location.href='text_image.php?file='+image_name;
我会用jQuery发送XMLHttpRequest(AJAX),因为window.location.href='text_image.php?file='+image_name;
isn't save. 不保存。 The user could go back in the browser history and visit this site again. 用户可以返回浏览器历史记录并再次访问该站点。
Also I think you haven't change the file name. 另外,我认为您尚未更改文件名。 move_uploaded_file($im,"upload_pic/angelina.jpg");
A small tip: Don't use the @ operator before functions! 一个小技巧:不要在函数前使用@运算符! It's a performance killer. 这是性能杀手。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.