2014-09-27 1 views
3

Для того, чтобы добавить текст к изображению, я использую ниже код с сайтаКак сохранить изображение после добавления текста в изображения

<?php 
    //Set the Content Type 
    header('Content-type: image/jpeg'); 

    // Create Image From Existing File 
    $jpg_image = imagecreatefromjpeg('sunset.jpg'); 

    // Allocate A Color For The Text 
    $white = imagecolorallocate($jpg_image, 255, 255, 255); 

    // Set Path to Font File 
    $font_path = 'font.TTF'; 

    // Set Text to Be Printed On Image 
    $text = "This is a sunset!"; 

    // Print Text On Image 
    imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text); 

    // Send Image to Browser 
    imagejpeg($jpg_image); 

    // Clear Memory 
    imagedestroy($jpg_image); 
?> 

Это работает хорошо, но я не могу сохранить в ПОС для текстовый файл экономия Я использую эту функцию

function write($post,$myFile){ 
    $fh = fopen($myFile, 'a+') or die("can't open file"); 
    fwrite($fh, $post); 
    fclose($fh); 
} 

есть ли способ, что я могу в состоянии сохранить изображение в формате JPG?

ответ

2

Это мой собственный код и хорошо работает! Просто измените имя name.jpg к имени файла , что вы хотите:

<?php 
    header('Content-type: image/jpeg'); 

    // Create Image From Existing File 
    $jpg_image = imagecreatefromjpeg('sunset.jpg'); 
//$jpg_image=imagecreatetruecolor(100,100); 

    // Allocate A Color For The Text 
$white = imagecolorallocate($jpg_image, 255, 255, 255); 


    // Set Path to Font File 
    $font_path = 'font1.TTF'; 

    // Set Text to Be Printed On Image 
    $text = "This is a sunset!"; 

    // Print Text On Image 
    $x=20; 
    for($i=0;$i<=strlen($text);$i++){ 
    $print_text=substr($text,$i,1); 
    $x+=20; 
    imagettftext($jpg_image, 30, 0, $x, 200, $white, $font_path, $print_text); 
    } 


    // Send Image to Browser 
    imagejpeg($jpg_image,'name.jpg'); 

    // Clear Memory 
    imagedestroy($jpg_image); 
?> 

Мой код немного отличается с вашими, один из отличается Вы не изменяли место указателя, то место, которое вы собираетесь поставить свой характер я имею в виду $ х:

imagettftext($jpg_image, 30, 0, $x, 200, $white, $font_path, $print_text); 

И еще отличается характер, вы дали строку (не символов) к imagettftext функция, но я даю один символ. Я думаю, что персонаж лучше, чем строка. В частности, для создания captcha.

0

Чтобы сохранить изображение в файле, а не выводить его, просто измените строку imagejpeg($jpg_image); на imagejpeg($jpg_image, 'yourfile.jpg'); (вы также можете удалить header('Content-type: image/jpeg'); в этом случае).

+0

не работает я так, как ты режиссер! – Yaldram

0

Проверьте этот код. Это то же самое, как ваш код, только разница в том,

imagejpeg($jpg_image,"imagefolderpath/image.jpg");

imagejpeg($jpg_image); вместо
Может быть, это полезно для вас.

<?php 
 
    header('Content-type: image/jpeg'); 
 
    $jpg_image = imagecreatefromjpeg('sunset.jpg'); 
 
    $white = imagecolorallocate($jpg_image, 255, 255, 255); 
 
    $font_path = 'font.TTF'; 
 
    $text = "This is a sunset!"; 
 
    imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text); 
 
    imagejpeg($jpg_image,"imagefolderpath/image.jpg"); 
 
    imagedestroy($jpg_image); 
 
?>

Смежные вопросы