Min width image scraper - php

I'm using an image scraping feature developed here: https://github.com/morshedalam/url-scraper-php
They are using this regular expression to find images:
private $img_expression = '/<img[^>]+src=([\'"])?((?(1).+?|[^\s>]+))(?(1)\1)/';
This is fine, however, it returns every single image (including tiny ones). Much like Pinterest, Facebook etc. I'm only interested in getting images that serve as thumbs i.e width > 200px. I realize that the dimensions of an image might not be defined in the html source.
How would you do this?
Cheers.

You need to download the extracted images, get their size and select those that are large enough.
Interestingly, there's an SO answer just for that: php get all the images from url which width and height >=200 more quicker

Related

Search and replace REGEX in post content for image filename

I've had an issue with importing images from Blogger to Wordpress, and somehow it used all medium sized dimensions for my inline post content images, which are way too small.
I've tried to resize my medium size dimensions in my Wordpress media settings, and then ran the Regenerate Thumbnails plugin, but for some reason the images in my blogger imported posts are still too small (using the old medium image size dimensions).
I've been trying to think of a way to fix this, and I think the easiest method would be to just search and replace in post content wherever there is an image tag that has dimensions on the end, like:
http://example.com/wp-content/uploads/2014/05/Comfy-Cozy-Couture-Design-Decoded-163x300.png
And replace that with:
http://example.com/wp-content/uploads/2014/05/Comfy-Cozy-Couture-Design-Decoded.png
By means of some sort of MySQL search and replace using REGEX. I want this to work no matter the image filename, and no matter the dimensions on the end.
So:
http://example.com/wp-content/uploads/2014/05/an-image-filename-123x456.png
would become:
http://example.com/wp-content/uploads/2014/05/an-image-filename.png
And also work regardless of extension (png/gif/jpg).
In an ideal situation, I wouldn't remove the dimension entirely, but just upsize the dimensions to my large media file size (which is max width: 600, max height: ∞) maintaining the same aspect ratio as the old dimensions. But I feel this sort of query is much more difficult. And I believe my user uploaded all web ready sized images to begin with.
Has anyone done this before or know of a fix for my problem? Or know of another simpler method without re-importing 2000+ posts from blogger?
Thanks in advance!
EDIT:
Or if someone knows why Regen. Thumbnails wouldn't work. My images are attached to the posts, but here is what my image tag looks like that's getting spit out:
<img alt="Comfy Cozy Couture" class="aligncenter size-full wp-image-3654" src="http://example.com/wp-content/uploads/2014/05/Comfy-Cozy-Couture-Design-Decoded-163x300.png" height="1100" width="600" />
It's odd because it has the correct dimensions in the width and height attrs but the image source has the old hardcoded dimensions in the filename.
Also previously I tried to do this all with JS but i wanted a more permanent solution. Here's what I was doing with JS:
$('.entry-content img').prop('src', function (i, v) {
return v.replace(/-\d+x\d+\.(jpg|png|gif)$/, '.$1');
});
I was able to perform a similar search and replace using the WordPress Search Regrex plugin (https://wordpress.org/plugins/search-regex/) and the following search pattern:
.-\d+x\d*.
If you try this, put .-\d+x\d*. in your search pattern and make sure you select "Regex" in the checkbox. I replaced the pattern with nothing (so essentially removing the "-###x###" portion of the string).
The plugin will allow you to preview the results before submitting. Search will show you what your pattern is doing. Replace will preview the replace. Replace and save does exactly what it sounds like :-)

Trouble with Interchange.js in Zurb Foundation when images are being dynamically resized by PHP class

I'm building a responsive site using Zurb Foundation.
I have a PHP script which will resize and caches an image using gdlib if you append a query string with new dimensions in the URL. For example to resize an image to 300px wide:
http://www.mydomain.com/images.php?imgfile=path/to/picture1.jpg&w=300
I am also using some HTACCESS rewrite rules to make this URL pretty and avoid having a query string. So this URL gives the same result as above:
http://www.mydomain.com/img/300w/path/to/picture1.jpg
The PHP file performs some simple arithmetic to constrain by width or height, checks if the resized version is already in cache, if so outputs it, if not, resizes the images, saves it using imagejpeg and outputs it with header("Content-type: image/jpeg");
I am also using Zurb Foundation and want to use the interchange javascript like so:
<img src="http://www.mydomain.com/img/300w/path/to/picture1.jpg"
data-interchange="[http://www.mydomain.com/img/300w/path/to/picture1.jpg, (default)],
[http://www.mydomain.com/path/to/picture1.jpg, (medium)]">
However, this does not seem to work. Only the 300px is shown for both breakpoints. After much testing it's clear that only what's in the src attribute is taking. The images passing through the resize script don't work. This is true even if it should be using the medium image which is the direct path the full size image.
I tried to debug the interchange javascript, but am not that skilled in Javascript.
Any help or advice would be appreciated. Someone must be trying to using dynamically resized images with PHP using interchange.js on a responsive site.
There is no need for debugging interchange, it works pretty well.
First, have you included the foudation.js file before interchange.js (dependancy) ?
Tip for debugging: try with default/medium/small and use different images (ex: different color rectangles) to quickly notice changes.
Also, in your example, there is only one path (see below) and you're having a "default" named-query. What is the point of loading the same image twice ? You might want your default size to be in src="", and your (typically) bigger sizes thereafter ?
What interchange does is letting the src"(ex: small.jpg)" loads as usual (hence it's displayed without js enabled) and THEN loads a bigger image depending on the named-query/media-query. So perhaps you could generate all your image size on upload (with no check for size existance needed). At least, it's the way I do it with wordpress.
<img src="http://www.mydomain.com/img/default-size/300w/path/to/picture1-small.jpg"
data-interchange="[http://www.mydomain.com/img/medium-size/800w/path/to/picture1-medium-sized.jpg, (medium)],
[http://www.mydomain.com/img/large-size/1200w/path/to/picture1-large-sized.jpg, (large)]">
As I can see on the Zurb Foundation Github repo Issues there may be a problem with url containing parameters and their regular expression

HTML5 Canvas Re-Colorize Images *Before* Rendering

I have an image which is essentially a star-burst effect. The color of the star-burst is white, and the background is transparent (PNG w/ Alpha). I randomly generate these star-bursts onto an HTML5 canvas at random locations, and at the same time, generate random Hue, Saturation, and Light (HSL) values. These could be RGB values, if this simplifies things.
The goal is to re-colorize the PNG for display on the HTML5 canvas based on the randomly generated HLS values before rendering it to the canvas.
I've read some other posts on StackOverflow and other sites, but the only solutions I've seen involve rendering it to the canvas, grabbing the coordinates for where the image is displaying, and modify the color on a pixel-by-pixel basis. In theory this could work, however some of the images may overlap slightly. Also if there is a background already present, then from what I understand, the background's color would also be modified which isn't much of a solutions for me either.
If this is out of the realm of what Canvases are capable of, as a fallback I suppose I would be okay with having images dynamically re-colored via PHP using GD2 or Imagick, or via the command-line via Gimp, ImageMagick or some other image library...
Thanks much!
-- OUTCOME --
Special thanks to #jing3142 for initial suggestion of off-screen canvas rendering, and #Jarrod for providing the vital piece I was missing: globalCompositeOperation = "source-in"
Here is a working implementation of the concept: http://jsfiddle.net/fwtW2/2/
Works in:
Chrome
Firefox
IE 9 (haven't tested other versions)
How about a second canvas that has the size of the image, place the image on that canvas, re-colour as required, extract re-coloured image and place at random on main canvas, re-colour image on second canvas using new HSL values, extract and randomly place on main canvas and repeat?
This may help as well https://developer.mozilla.org/en-US/docs/HTML/Canvas/Pixel_manipulation_with_canvas
I'm pretty sure you can just use the source-in globalCompositeOperation opertaion? No need to get all hardcore and crazy with vector images.
This code is where the magic happens:
var color = 'red';
ctx.fillStyle = color;
ctx.globalCompositeOperation = "source-in";
But yOu'd need to re-draw this to an offscreen canvas: You can do that like so
createBuffer = function(sizeW, sizeH)
{
buffer = document.createElement('buffer');
bufferCtx = buffer.getContext('2d');
buffer.width = sizeW;
buffer.height = sizeH;
}
then just draw your image to the offscreen canvas and apply the global composition.
Here’s how to easily recolor your starbursts just before rendering on canvas
Convert your starburst.png into a vector path! (I know, you will have to drag out Gimp/Illustrator for this one time—but it’s worth it!)
Starburst paths can have fills and the fills can be solid colors or even gradients—as fancy, random recoloring as you need!
Starburst paths can scale without the pixilation of raster images like .png’s.
Starburst paths can rotate to show diversity in your bursts--and even skew to represent a bit of 3d motion.
If you want to get fancy, you can even create “glowing” bursts by using shadows and blurs.
Starburst paths are faster to render than .png images—especially if you need to manipulate each .png before drawing.

resize the_post_thumbnail to stretch properly, wordpress

Hi Im using the_post_thumbnail for the nivo slider, my issue is that I cant seem to find a nice easy way to automatically resize the images to exact dimensions.
When I use add_image_size it doesnt stretch it correctly, it keeps the constraint on. I dont want cropping or anything. I just want the user to upload the picture and not have to do anything after that.
Its probably a stupid but I have googled it and even though there is lots about it, including various plugins, I cant seem to find a solid answer to this.
Thanks
I think you need to clarify your question. If I understand correctly you want the uploaded image to be the exact image without cropping? Make sure that the upload is saved before calling the_post_thumbnail function of the nivo slider. That way you have the raw image to manipulate before the function gets it.
The add_image_size()-method has a flag for the crop method you wish to use. both of them will keep the proportions of the image intact however.
You could extend the functionality or build your own plugin to handle the resizing in the way you want - you may want to take a look at /wp-includes/media.php and a simple resizing class like https://github.com/mdular/ReSample.php/blob/master/ReSample.php
I'm not a big fan of distorting the image proportions though. If the client is unable to differentiate between a portrait or landscape aspect ratio then cropping the image rather than stretching it seems like the more elegant solution in most cases.
For a quick & dirty solution to your problem though i suggest the following:
add the image size and contrain it by the width OR height to fit your sliders width OR height (this example constrains only by width):
add_image_size( 'slider-image', 760, 9999 ); //760 pixels wide, "unlimited" height
when outputting the image tags, force the dimensions to 760 x 474 in the image tag
< img src="" width="760" height="474" ...>
This will leave you with image files that are potentially taller or wider than what you want but will be forced to the set dimensions in the browser, and therefore fit the slider. It's dirty, yes, but it will work with the tools you currently have.
Update
in response to your comment: you can construct the image tag yourself by getting the raw image info.
1) grab the post meta, like so (assuming the_post() is already executed, it should be if you are in the while-loop):
$meta = get_post_meta($post->ID)
2) check for the thumbnail id to avoid empty output (and do steps 3 + 4 inside the if-block)
if(!empty($meta['_tumbnail_id'][0])
3) grab the actual thumbnail raw data - be sure to fill in the 'THUMBNAIL VERSION' (size) you want ('thumbnail', 'medium', etc.. or your own if you used add_image_size() in your functions.php)
$image = wp_get_attachment_image_src( $meta['_thumbnail_id'][0], 'VERSION');
4) construct the image tag:
$image[0] = the src url
$image[1] = the width - you may use this
$image[2] = the height - you may wish to force this to your liking
hope this helps :)

Best Youtube embed player sizes?

I use tumblr to share videos and, unfortunately, when re-posting a video to your tumblelog it uses the embed code at 400x336px. This is neither widescreen nor very large.
So I'm trying to set the player to better size and I'm finding that the youtube player really comes in many sizes. For example, when copying the embed code for and HD video I get the sizes 560x340, 640x385, and 853x505. Then when I look at the embed code for a SD video I get the sizes 425x344, 480x385, and 640x505.
I was having the best luck with setting the player to 640 x 505 as it was large enough for HD and perfect for SD.
print str_replace( array('400', '336'), array('640', '505'), $video_player_html);
Has anyone dealt with trying to find a good standard embed size for a site? One thing that would make this a lot easier is to know whether the video was HD or not - however, that info is not provided in the embed code.
I guess I am actually more interested in knowing the right ratio (or specific size) to use that works the best for youtube's mixed content.
Update
Since HD seems to be .5625 ratio (16/9) I'm thinking that I can just decide on a hight and multiply it by the ratio to get the proper width each time (plus 23px for youtube bar).
You don't have to worry about the sizes youtube offers. When you copy the embed code, paste it into a text editor and you'll see it contains width and height parameters in two places. You can change those values to anything at all and the player will resize. So grab your JRuler, figure out what size would be ideal for your tumblelog, and just edit the player code each time.
It's kind of a pain, but it gets you the result you want.

Categories