PHP: How to get all images from an HTML page

rgbstock.com

I was curious to how I could make something similar to what Facebook does when you add a link. Somehow it loads images found on the page your link leads to, and then it presents them to you so you can select one you want to use as a thumbnail.

Well, step one to solve this is of course to find all the images on a page, and that is what I will present in this post. It will be sort of like a backend service we can use later from an AJAX call. You post it a URL, and you get all the image URLs it found back. Let’s put the petal to medal!

Continue reading

PHP: Dealing with absolute and relative URLs

I’m currently writing a post on how to get image tags from a remote HTML page using PHP. One sticky issue with that is that the image URLs you find is a joyful mix of absolute and relative URLs.

Luckily, I found a function on nashruddin.com which seem to handle them alright. After a bit of clean up and fixing an error, we have this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
function make_absolute($url, $base)
{
    // Return base if no url
    if( ! $url) return $base;

    // Return if already absolute URL
    if(parse_url($url, PHP_URL_SCHEME) != '') return $url;
   
    // Urls only containing query or anchor
    if($url[0] == '#' || $url[0] == '?') return $base.$url;
   
    // Parse base URL and convert to local variables: $scheme, $host, $path
    extract(parse_url($base));

    // If no path, use /
    if( ! isset($path)) $path = '/';
 
    // Remove non-directory element from path
    $path = preg_replace('#/[^/]*$#', '', $path);
 
    // Destroy path if relative url points to root
    if($url[0] == '/') $path = '';
   
    // Dirty absolute URL
    $abs = "$host$path/$url";
 
    // Replace '//' or '/./' or '/foo/../' with '/'
    $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
    for($n = 1; $n > 0; $abs = preg_replace($re, '/', $abs, -1, $n)) {}
   
    // Absolute URL is ready!
    return $scheme.'://'.$abs;
}

I can sort of read through and see what it does, but I can’t explain it very well. So, I’ll just leave it at that. So far it has worked fine for me. Maybe some corner cases that are missing, and if there are, please let me know!

:idea: What I added to the original function was line 5 and 17. The first to prevent it from crashing if the url is null or empty, and the second to prevent it from crashing if parse_url finds no path. For example if the url is http://www.example.com (No /whatever at the end).

The base tag

A tag that is easy to forget about is the base tag. The above function gets the base path from the URL given as base. For example if you gave it http://www.example.com/directory/file.html as base, it would use http://www.example.com/directory/. However, if file.html included the following base tag:

<base href="http://www.example.com/">

Then the base path would be http://www.example.com/ instead. Fun, eh?

As long as you know about it, it’s not to hard to deal with though. You just need to get a hold of it and provide that as base instead when using the function above.

Works On My Machine™! And if it doesn’t on yours, let me know. If it’s a mistake in the function, I’d like to fix it!

PHP: How to proportionally resize an uploaded image

Big mug and tiny mug

rgbstock.com

Say you have a form where someone can upload a profile image. The uploaded image can be of any size of course, but you want all the profile images to fit inside a certain frame. You could just set the dimensions on the image tag to this size, but in most browsers that would look ugly, and it would also most likely stretch the image. It would look awful. In addition you would be serving a image which most likely was a lot larger than you wanted it to be. This would cost you bandwidth.

In this case you might want to proportionally resize the image to the appropriate size when you get it uploaded. You can then store the resized image instead and serve it directly with no problems afterwards. It doesn’t have to be difficult! Here’s how :)

Continue reading

Failure should be an option

Just wanted to share a blog post I stumbled over on Seth’s blog. Since it was very short and very good (and I don’t want to lose it), I’ll just quote the whole thing here.

The flip side

It’s impossible to have a coin with only one side. You can’t have heads without tails.

Innovation is like that. Initiative is like that. Art is like that.

You can’t have success unless you’re prepared to have failure.

As soon as you say, “failure is not an option,” you’ve just said, “innovation is not an option.”

Good point and well said! Definitely something to think about. I’d say this applies both in software development and life in general. And probably in a lot of other areas you might think of :)

Unix: Recursive search for text in files

Silhouette of a person with binoculars against a sunset

rgbstock.com

Keep forgetting how to do this, so here it is. How to do a quick and simple, recursive search for text using grep. Hint: It’s very simple.

$ grep -rl "some text" .

That’s all there is to it! grep is the command, r makes the search recursive (i.e. in the target folder and all its sub-folders), l means it will list the name of all the files where it finds the text, "some text" is the string to search for, and finally, the dot at the end means to start the search in the current directory. And just to clarify, this searches for text inside of files, in the content of the files; not the filenames.

There, now I know where to find it when I forget it next time. And perhaps I have helped someone else too :)

Good night!

Oracle: How to search for a table or column in a big database

I needed to find columns containing a certain string. That is, the name of the column should contain that string. Actually not too difficult to do :)

SELECT TABLE_NAME, column_name
FROM all_tab_columns
WHERE column_name LIKE '%FOO_ID%';

That gives you a nice list of all columns containing FOO_ID and what tables you find them in. Fantastic! Now, back to work…

Professionalism and the Four-Way Test

rgbstock.com

Stumbled over another interesting blog post called the Professionalism and the Four-Way Test. It consists of the following four questions:

  1. Is it the truth?
  2. Is it fair to all concerned?
  3. Will it build goodwill and better friendships?
  4. Will it be beneficial to all concerned?

Most definitely something to ask yourself and to reflect upon once in a while; both in your profession and in general life.