Php image to str

imagecreatefromstring

imagecreatefromstring() возвращает идентификатор изображения, представляющего изображение полученное из потока image . Эти типы будут автоматически определяться, если сборка PHP их поддерживает: JPEG, PNG, GIF, WBMP и GD2.

Список параметров

Строка содержащая данные изображения.

Возвращаемые значения

В случае успеха будет возвращен ресурс изображения, FALSE , если тип изображения не поддерживается, данные не распознаются или данные нарушены и не могут быть загружены.

Примеры

Пример #1 Пример использования imagecreatefromstring()

$data = ‘iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl’
. ‘BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr’
. ‘EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r’
. ‘8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==’ ;
$data = base64_decode ( $data );

$im = imagecreatefromstring ( $data );
if ( $im !== false ) header ( ‘Content-Type: image/png’ );
imagepng ( $im );
imagedestroy ( $im );
>
else echo ‘Произошла ошибка.’ ;
>
?>

Результатом выполнения данного примера будет что-то подобное:

Смотрите также

  • imagecreatefromjpeg() — Создает новое изображение из файла или URL
  • imagecreatefrompng() — Создает новое изображение из файла или URL
  • imagecreatefromgif() — Создает новое изображение из файла или URL
  • imagecreatetruecolor() — Создание нового полноцветного изображения

Источник

Php turn image to string php

The output will be: Solution 3: if it is valid XML, you can use about any decent XML parser (SimpleXML for example), unmarshall the source, traverse the tree looking for image tags, try lookup the attribute and set it if it does not exist, and the remarshall the xml . should be a 10-liner or so . good luck . Solution 2: try: Solution 3: Replace this regex with empty string Question: Current image looks like Now if do not have the , the code itself will replace the image like How can be done with php? Solution 1: The quickest solution, assuming images always follow that format, is this: However, I would question your motives.

How to remove first image from string

I have the results from a database that contains images and text. I would like to remove the first image.

$string = 'This is some text and images , more text and  and so on'; 

I would like to remove the first image, it is not always the same url.

$string = 'This is some text and images , more text and  and so on'; print preg_replace('//i','',$string,1); 
This is some text and images , more text and and so on 

Assuming you know it’ll be prefixed by spaces and a line break, and suffixed by a comma and line break (and you want to remove these, too), you can do

print preg_replace("/\n \,\n /i",'',$string,1); 
This is some text and images more text and and so on 

There was a great answer on another Thread

function get_first_image($html)< require_once('SimpleHTML.class.php') $post_dom = str_get_dom($html); $first_img = $post_dom->find('img', 0); if($first_img !== null) < return $first_img->src; > return null; > 

You can do it via Regex expressions however regex isn’t really suited for this.

$var = 'This is some text and images , more text and  and so on'; echo preg_replace('//', '123', $var, 1); 

This should do it. ? in the regex is to make it ungreedy.

Being a RegEx newbie, I tend to avoid it and use something like:

$i = strpos($string,'', $i); if ($j !== false) $string = substr($string,0,$i) . substr($string,$j); > 

Probably should be $i-1 and/or $j+1 — I never remember exactly what it should be.

Php — Remove image from string based on src url, I am looking for an easy and efficient way to remove a specific image from an article. All that I know is the image URL of the image that I need to remove. The image may or may not use different attributes. The image may or may not exist at all in the article. There might be other images (not same url) in the article.

Remove image from string based on src url

I am looking for an easy and efficient way to remove a specific image from an article. All that I know is the image URL of the image that I need to remove.

  • The image may or may not use different attributes.
  • The image may or may not exist at all in the article.
  • There might be other images (not same url) in the article.

My choice would be either regex or DOMDocument , probably using an HTML5 parser like https://github.com/Masterminds/html5-php.

My regex skills are not that good, and I’m not sure if it’s a good idea to use regex to accomplish this because I read that regex should be avoided to parse HTML. What I have with so far with regex, is to remove the complete image, but not sure how to remove it based on a specific src url.

$img_src = 'http://www.example.org/image_to_be_removed.jpg'; $article = '

Test article with HTML5 tags

This is an example article. The article may or may not include html5 tags, images and other things.

More example text.

'; $article = preg_replace("/]+\>/i", "", $article); echo $article;

I haven’t dug into the DOMDocument solution yet, because I am not sure if it’s even possible or if regex might be considered best practice?

$article = preg_replace("/]+src=\"" . preg_quote($img_src, '/') . "\"[^>]*\>/i", "", $article); 

You can try this. It seems to test ok. At any rate it should give you an idea as to how to proceed.

$img_src = 'http://www.example.org/image_to_be_removed.jpg'; $article = '

Test article with HTML5 tags

This is an example article. The article may or may not include html5 tags, images and other things.

\s/'; //Define the regex. $article = preg_replace($regex, '', $article); echo $article;

You can try below with str_replace

Test article with HTML5 tags  

This is an example article. The article may or may not include html5 tags, images and other things.

More example text.

'; $new = str_replace('src="http://www.example.org/image_to_be_removed.jpg"','',$article); echo $article; echo '
'; echo $new; ?>

there is both preg_replace from your code and str_replace,to notice deference. There are other function to do the same like sprintf,strtr,str_replace and preg_replace you can use whichever suites

It is not recommended to parse html with regex.

As you suggested, you might for example use DOMDocument or for example PHP Simple HTML DOM Parser.

Because you state that «All that I know is the image URL of the image that I need to remove», you could find the src attribute of the img tag using xpath or looking for the tag name and check that.

$img_src = 'http://www.example.org/image_to_be_removed.jpg'; $article = '

Test article with HTML5 tags

This is an example article. The article may or may not include html5 tags, images and other things.

More example text.

\';

More example text.

'; $dom = new DOMDocument(); libxml_use_internal_errors(true); $dom->loadHTML($article); $xpath = new DOMXPath($dom); $elements = $xpath->query("//img"); foreach ($elements as $elememnt) < if ($elememnt->getAttribute("src") === $img_src) < $elememnt->parentNode->removeChild($elememnt); > > echo $dom->saveHTML();

Example PHP Simple HTML DOM Parser using simple_html_dom.php :

$htmlDom = str_get_html($article); foreach($htmlDom ->find('img[src=http://www.example.org/image_to_be_removed.jpg]') as $item) < $item->outertext = ''; > $htmlDom->save(); echo $htmlDom; 

PHP Object to String | How to Convert Object to String in, There is no exact syntax which is extensively used to convert PHP object into String but there are other ways to achieve it for those conversions, syntax exist and are used by embedding them within the code which is represented as follows : $ var = some_name $var = obj_1; < Call function with $var; Use _toString () method; >…

Remove ‘img’ tag from string using preg_replace [duplicate]

Possible Duplicate:
PHP — remove tag from string

I have my content like this:

$content = '

 

Php image to str

An explorer adventures into an unknown world, yet it seems that he has been there before. A short animated film directed by Malcolm Sutherland in 2010. With music by Alison Melville and Ben Grossman, and foley by Leon Lo. Sound design / mix by Malcolm Sutherland.

The animation is all hand-drawn; a mix of drawing / pastels on paper and digital animation with Toonboom Studio and a wacom cintiq tablet, assembled in After Effects 7 and edited in Sony Vegas 8.

 

';

I want the output ignoring the tag. I tried some messups with preg_replace but didn’t work.. It will be a great help if someone can explain me how it works.

If you are not forced to use regular expressions to parse HTML content, then I’d suggest using PHP’s strip_tags() function along with its allowed_tags argument.

This will tell PHP to remove all the html tags and leave the ones your specified in the allowed_tags .

Example usage taken from the documentation —

$text = '

Test paragraph.

Other text'; echo strip_tags($text); // output : Test paragraph. Other text // Allow

and echo strip_tags($text, '

'); // output -

Test paragraph.

Other text

So, if you simply specify all the HTML tags in allow_tags except the tag , you should get the results that you need — only removing the tag from your string.

 $content = preg_replace("/]+\>/i", " ", $content); 

Replace this regex )|()) with empty string

How to convert a PHP object to a string?, Catchable fatal error: Object of class stdClass could not be converted to string So, my question is, how do I convert an object to a string in PHP? I don’t want to serialize it though. Just a note: the code I use works in PHP 4, but not in PHP 5. Thanks! EDIT: I resolved it myself. It was a pain, but I did it. Thanks …

PHP — How to replace empty alt tag on image

Now if img src do not have the alt=»» , the code itself will replace the image like

The quickest solution, assuming images always follow that format, is this:

$search = '//'; $replace = 'Php image to str'; $code = preg_replace( $search, $replace, $code ); 

However, I would question your motives. Adding a blank alt tag is pretty pointless.

Here’s an example of doing this with SimpleXML.

    '; $xml = new SimpleXMLElement($html); foreach ($xml->xpath('//img') as $img) < if (!array_key_exists('alt', $img->attributes())) < $img->addAttribute('alt', ''); > > echo $xml->asXML(); 
    Php image to str  

if it is valid XML, you can use about any decent XML parser (SimpleXML for example), unmarshall the source, traverse the tree looking for image tags, try lookup the attribute and set it if it does not exist, and the remarshall the xml .

should be a 10-liner or so .

PHP | imagestring() Function, The imagestring () function is an inbuilt function in PHP which is used to draw the string horizontally. This function draws the string at given position. Syntax: bool imagestring ( $image, $font, $x, $y, $string, $color ) Parameters: This function accepts six parameters as mentioned above and described below:

Источник

php — Convert image to string without saving image

Solution:

You can use any of the image* functions that output an image as a stream and use output buffering to capture it:

ob_start(); // example for jpeg imagejpeg($resource); $image_string = ob_get_contents(); ob_end_clean(); 

Share solution ↓

Additional Information:

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Источник

Читайте также:  Free profiler for java
Оцените статью