replace width of image while inserting into db - php

I have a blog system where user inputs the image url in the post content like
hey how are you <img src="example.com/image.png" style="width: 952px;">
if the user has written like this
hello how are you <img src="example.com/image.png" style="width: 952px;">
Then I want to find this style width 952px line and replace it into 100%
the user can input any dimension of image like 100px 300px
here is what I have tried:
$content is what user have posted
$go = $content;
$mystr= $go;
$start=strpos($mystr,'style="width: ');
$end=strpos($mystr,'">');
$jo = substr($mystr,0,$start+strlen('') ) . 'style="width: 100%;' .
substr($mystr,$end);
The problem i'm facing is that if user puts two or three image tag but this script is only replacing one width value how to replace multiple width
the user inputs->
<img src="example.com/img.png" style="width: 500px;"><br><img
src="example.com/img2.png" style="width: 952px;">
here what i got as result
<img src="example.com/img.png" style="width: 100%;"><br><img
src="example.com/img2.png" style="width: 952px;">
second image did'nt changed value of width

Use preg_replace function:
$go = preg_replace('/style="width:\s*\d+px;/i', 'style="width:100%;', $content);
That's assuming $content has only your <img> tags, otherwise you may mess up other html elements.

This case is perfect for regular expression search and replace.
In PHP you would use preg_replace , documentation for which you can find here:
- http://php.net/manual/en/function.preg-replace.php

Just use a regex with preg_replace() to replace the width in each img tag, e.g.
echo preg_replace("/<img.*?\Kwidth:\s*[^;]*/s", "width:100%", $input);
Simply exanied we first match everything until the width attribute in a img tag (<img.*?) and then reset the match with \K so we then can match the width attribute(width:\s*[^;]*/) and replace it with width:100%.

Related

preg_replace for images in PHP

I have a question about preg_replace. I have the following HTML in WordPress:
<img width="256" height="256" src="http://localhost/wp-content/uploads/2015/08/spiderman-avatar.png" class="attachment-post-thumbnail wp-post-image" alt="spiderman-avatar">
I change it to the following:
<img src="" data-breakpoint="http://localhost/wp-content/uploads/2015/08/" data-img="theme-{folder}.jpg" class="srcbox" alt="spiderman-avatar">
with the following preg_replace:
$html = preg_replace(
'/src="(https?:\/\/.+\/)(.+\-)([0-9]+)(.jpg|.jpeg|.png|.gif)"/',
'src="" data-breakpoint="$1" data-img="$2{folder}$4"', // Replace and split src attribute into two new attributes
preg_replace(
'/(width|height)="[0-9]*"/',
'', // Remove width and height attributes
preg_replace(
'/<img ?([^>]*)class="([^"]*)"?/',
'<img $1 class="$2 srcbox"', // Add class srcbox to class attribute
$html
)
)
);
I have the feeling I have written some serious slow code, and it can be done in a single preg_replace.
Chris85 mentioned the HTML parser, so I found this and got this so far:
http://nimishprabhu.com/top-10-best-usage-examples-php-simple-html-dom-parser.html
include('simple_html_dom.php');
$html = file_get_html($html);
From here I COULD loop through all images and change the th attribute. But how do I put the new element were it came from?
you should better use DOM
http://php.net/manual/de/domdocument.loadhtml.php
and extract the attributes with it.

Remove specific image from a string

I want to remove specific image from string.
I need to remove Image with specific width and height.
I have tried this, but this will remove first image.
$description = preg_replace('/<img.*?>/', '123', $description, 1);
I want to remove any/all image(s) with specific width and height.
E.g. Remove this image <img width="1" height="1" ..../>
I suggest that you move away from using regex expressions to parse (or manipulate) HTML, because it's not a good idea, and here's a great SO answer on why.
For example, by using Peter's approach (preg_match_all('~<img src="(.+?)" width="(.+?)">~is', $content, $return);), you are assuming that all your images start with <img, are followed by the src, and then contain the width=, all typed exactly like that and with those exact whitespace separations, and those particular quotes. That means that you will not capture any of these perfectly valid HTML images that you want to remove:
<img src='asd' width="123">
<img src="asd" width="123">
<img src="asd" class='abc' width="123">
<img src="asd" width = "123">
While it's of course perfectly possible to catch all these cases, do you really want to go through all that effort? Why reinvent the wheel when you can just parse the HTML with already-existing tools. Take a look at this other question.
Made a little example for you
<?php
$string = 'something something <img src="test.jpg" width="10" height="10" /> and something .. and <img src="test.jpg" width="10" height="10" /> and more and more and more';
preg_match_all('~<img(.+?)width="10"(.+?)height="10"(.+?)/>~is', $string, $return);
foreach ($return[0] as $image) {
$string = str_replace($image, '', $string);
}
echo $string;
I got the solution:
$description = preg_replace('!<img.*?width="1".*?/>!i', '', $description);

opencart resize images in a string containing html

I have a string from a customers signature in the variable $signature .
The string is html and will probably contain tags.
How can i resize all the images within that string to a maxmimum set size to stop people including massive pictures but still have all the html with resized tags in the variable $signature.
I would need to set either a maximum size or some sort of ratio to reduce the image displayed.
example string :
$signature = '<b>test text</b><img src="http://kushsrivastava.files.wordpress.com/2012/11/test.gif">'
Any help would be brilliant thanx..
Perhaps you could wrap it all in a style and add a fix to the stylesheet.
so when you output the sig on the page, you wrap it...
<div class="sigstyle">
<?php echo $signature; ?>
</div>
Then, in your stylesheet, add a class...
.sigstyle img {
max-width: 300px;
max-height: 300px;
}
Just use the CSS property to re-size your image
$signature = '<b>test text</b><img src="http://kushsrivastava.files.wordpress.com/2012/11/test.gif" width="100px" height="100px">';

find image with specific src using preg_replace

I have some text with images within it. I want to replace specific images within the text with something else.
i.e. the text contains an a youtube img url that I want to replace with the actual video link.
<img class="mceItem" src="http://img.youtube.com/vi/1MsVzAkmds0/default.jpg" alt="1MsVzAkmds0">
and replace it with the youtube Iframe code:
<iframe title="'.$id.'" class="youtube-player" type="text/html" width="576" height="400" src="http://www.youtube.com/embed/'.$id.'" frameborder="0"></iframe>
my function looks like this:
function replacelink($link) {
$find= ("/<img src=[^>]+\>/i");
$replace = youtube("\\2");
return preg_replace($find,$replace);
}
What do I need to change in the regex to do the above?
Your regex is looking for <img src=, but there is a class attribute between img and src. Using $find= '/<img.*src=[^>]+>/i'; corrects the problem; however, this illustrates why you shouldn’t use regex to parse HTML.
You wrote:
I have some text with images within it.
If the text you’re referring to is actually HTML, then there are better alternatives to using regex for this.
Update
I believe this is what you’re looking for.
<?php
function replacelink($text) {
$replace = '<iframe title="$2" class="youtube-player" type="text/html" width="576" height="400" <iframe title="$2" class="youtube-player" type="text/html" width="576" height="400" src="http://www.youtube.com/embed/$2" frameborder="0"></iframe>';
$find = '/(<img.*?alt="([\da-z]+)".*?>)/i';
return preg_replace($find, $replace, $text);
}
$imagestr = '<img class="mceItem" src="http://img.youtube.com/vi/1MsVzAkmds0/default.jpg" alt="1MsVzAkmds0">';
echo replacelink($imagestr);
?>
There’s no need for a separate youtube() function.
If you want to replace more than one image, use preg_replace_all() instead of preg_replace().
The following regex would get all the images with a specific url. I not sure if this is what you wanted.
<img [^>]*?src="url"[^>]*?>
Previous anwser would fail if there were more than one image.

Replacing the image within an image src tag

I have the following situation, I have an html file with image tags about 10 or more, but it varies. Now what I want to achieve is to replace the image src with a PHP function as example below
I want to replace something like this
<img src="image1.png" ....
<img src="image2.png" ....
with this
<img src="<?=imageResize('image1.png',20,15)?>" ...
<img src="<?=imageResize('image2.png',20,15)?>" ..`.
Is this possible?
Let's assume that the current page source is contained in $source. Try some regular expressions:
<?php
preg_replace ('/<img src="(.+)"/Ui', '<img src="<?=imageResize(\'\\1\',20,15)?>"', $source);
?>
I see 2 cases:
Your imageResize function resizes the image, creating a new one with a different name (something like image120x15.png) and returning a string with the image name. In this case you should do something like:
<img src="<?php echo imageResize('image1.png',20,15);?>" ...
Your imageResize actually resizes the initial image and overriding it. In this case, do something like:
<?php imageResize('image1.png',20,15);?>
<img src="image1.png" ...
Capture output with output buffer and then apply regex for replace.
<?ob_strart();?>
<img src="image1.png" />
<img src="image2.png" />
<?php
$cnt = ob_get_clean();
$cnt = preg_replace ('/<img src="([^"]+)"/', '<img src="<?=imageResize(\'\\1\',20,15)?>"', $cnt);?>
For execute function imageresize you must run output in eval().

Categories