2013-05-21 34 views
0

我正在使用PHP写图片上的图片。我很容易从当前图片创建新图片并使用PHP写文本。但我也希望当我点击应用更改时,将这个新图片移动到我的目录(move_uploaded_file)并用新图像替换当前图像,新图像名称必须与之前图像相同,因为我正在使用PHP下载它。从@imagecreatefromjpeg获得<img>标记?

这是我用来写它的代码。

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代码:

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

<?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; 
    } 
?> 

在先进的感谢!

+0

我检查响应这是非常复杂的JFIF> CREATOR:GD-JPEG V1.0(使用IJG JPEG V62),默认质量 Ç\t \t $。' ”,#(7),01444'9 = 82 <.342C \t \t \t 2 !! 22222222222222222222222222222222222222222222222222" \t }!1AQa“Q2#BR $ 3房 – CuteBabyMannu

+2

这不是‘复杂’,这正是一个JPEG图像看上去,当你在文本模式下查看等。 – Spudley

+1

另外,要小心使用'$ _FILES [“image_file”] [“name”] - 因为文件的名字由客户端决定,所以它有可能被用作攻击媒介。包含斜线,你的代码可能会加载一个不同的文件到你期望的文件中,你应该确保你在使用之前对它进行了清理,或者根本不使用它(即为你上传的文件构建你自己的名字)。 – Spudley

回答

0

我会发送一个XMLHttpRequest(AJAX)与jQuery,因为​​ 不保存。用户可以返回浏览器历史记录并再次访问该网站。

另外,我认为你没有改变文件名。 move_uploaded_file($im,"upload_pic/angelina.jpg");

小提示:不要在函数之前使用@运算符!这是一个性能杀手。

+0

这只是我正在检查的文本编写代码。我将使用AJAX来做到这一点。 – CuteBabyMannu

+0

如何移动这个文件,因为这将是这个新创建的文件的名称,这将是我已经更新了我的代码为Ajax – CuteBabyMannu

+0

,但它不工作 – CuteBabyMannu

1

您想要在更改时重新加载IMG源。所以,你需要做的就是更换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()); 
    } 
}); 
}); 

脚本需要输出什么的PHP,刚刚改写图像文件

<?php 

    $file = 'upload_pic/'.$_GET['file']; 
    $im = writeToImage($file, 'PHPRO rules again'); 

    imageJPEG($im, $file, 95); // 95% quality 

    die(); 

    ... 
?> 

尽管如此,您可能希望输出JSON,指出“成功”或“失败”。

您还想检查$_GET['file']是有效的,可访问的,允许的图像。例如,你可能会迫使该文件是一个基本名称:

$file = 'upload_pic/'.basename($_GET['file']); 
    if (!file_exists($file)) 
     // ...no such file... 
    if (false === getImageSize($file)) 
     // ...doesn't look like an image... 
    // etc. 

另一种方式

如果你想保留原件和“字幕”的形象,你需要create a new file,因此输出的新名字以jQuery可以替换SRC属性。

例如:

$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}\"}"); 

jQuery中,data将现在包含$new,所以:

success:function(data){ 
     jQuery('#image').attr('src', data.src); 
    } 

(在页面中,您可能需要更新等领域,如果你想重新标题已标题图像)。

+0

我使用你的第一个方式不对tmp_name – CuteBabyMannu

+0

如果你调用会发生什么来自浏览器的PHP脚本(即不通过AJAX)具有相同的'file ='参数值?这应该给出一些提示,指出发生了什么问题。 – LSerni

+0

@iserni第一种方式工作正常... :)谢谢它,但我想kepp文件原来和新的,所以我使用第二种方式,但在第二种方式新图像创建,但这个图像是空白。 我只是在** text_image.php **中更改此代码** '$ unique_id = rand(); jQuery('#image')。attr('src',data);''''''''''';''''''''''';' 并在主文件中的此代码 ' – CuteBabyMannu