get_images_from_uri

Definition:
function get_images_from_uri($uri) {}

Retrieve all image URLs from given URI.

Parameters

  • string $uri

Source code

		function get_images_from_uri($uri) {

			$uri = preg_replace('/\/#.+?$/','', $uri);

			if ( preg_match('/\.(jpg|jpe|jpeg|png|gif)$/', $uri) && !strpos($uri,'blogger.com') )

				return "'" . esc_attr( html_entity_decode($uri) ) . "'";

			$content = wp_remote_fopen($uri);

			if ( false === $content )

				return '';

			$host = parse_url($uri);

			$pattern = '/<img ([^>]*)src=(\"|\')([^<>\'\"]+)(\2)([^>]*)\/*>/i';

			$content = str_replace(array("\n","\t","\r"), '', $content);

			preg_match_all($pattern, $content, $matches);

			if ( empty($matches[0]) )

				return '';

			$sources = array();

			foreach ($matches[3] as $src) {

				// if no http in url

				if (strpos($src, 'http') === false)

					// if it doesn't have a relative uri

					if ( strpos($src, '../') === false && strpos($src, './') === false && strpos($src, '/') === 0)

						$src = 'http://'.str_replace('//','/', $host['host'].'/'.$src);

					else

						$src = 'http://'.str_replace('//','/', $host['host'].'/'.dirname($host['path']).'/'.$src);

				$sources[] = esc_url($src);

			}

			return "'" . implode("','", $sources) . "'";

		}

1422

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: