duanoucuo7045 2013-05-21 10:23
浏览 338

从@imagecreatefromjpeg获取<img>标签?

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.

Here is my code which I am using to wrote text over it.

HTML code :

     <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('#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;
    }
?>

Thanks in Advanced!

  • 写回答

2条回答 默认 最新

  • drnrxv9383 2013-05-21 10:29
    关注

    I would send a XMLHttpRequest (AJAX) with jQuery, because 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行