• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 579
  • Last Modified:

PHP open external thumbnail image and save to local

First off, I'm a complete newbie at PHP and trying to learn...

That being said, I'm trying to index remote thumbnails.  The problem is, the remote site sometimes doesn't have the numbers in sequence (ie.. 001, 002, 003, 004, 005, etc..).  So my orginal code failed due to missing thumbnails.  I'm tryign to work around it by setting up an array of possible chocies.  The remote site has 20 thumbnails, I only need 10.  So if 001 doesn't exist, then I want to get 002, etc..

On the local site, I need to write out the captured thumbnail in sequence..  (i.e. 001.jog is written to -1.jpg or if 001 doesn't exist, get 002 and write it to xxx-1.jpg then get 003 and write it to xxx-2.jpg, etc..

This is the code I've written so far but doesn't work..   Spending way too much time on it and was hoppign someone here could help?
<?php
 
	function download_file( $file_source, $file_target )
	{
		$tc = 0;
		$orgThumb = array('01.jpg','02.jpg','03.jpg','04.jpg','05.jpg','06.jpg','07.jpg','08.jpg','09.jpg','10.jpg','11.jpg','12.jpg','13.jpg','14.jpg','15.jpg','16.jpg','17.jpg','18.jpg','19.jpg','20.jpg');
		$newThumb = array('-1.jpg','-2.jpg','-3.jpg','-4.jpg','-5.jpg','-6.jpg','-7.jpg','-8.jpg','-9.jpg','-10.jpg');
		
		for($t=0;$t<count($orgThumb);$t++){
			if ($tc < 10) {
		
				$soure = ($thumb_url. $orgThumb[$t]);
				$target = ($file_target .$newThumb[$tr]);
	
			if ( file_exists( $target ) )
			{
				unlink ($target);
			}
 
		$rh = fopen( $file_source, "rb" );
		if ( $rh === false)
		{
			$tc++;
		}
			}
		
		$wh = fopen( $target, "wb" );
		if ( $wh === false )
		{
			return false;
		}
		while ( !feof( $rh ) )
		{
			if ( !( fwrite( $wh, fread( $rh, 1024 ) ) === FALSE ) )
			{
				continue;
			}
			return false;
		}
		fclose( $rh );
		fclose( $wh );
		}
		return true;
	}
		$thumb_url = "http://thumbs.redtube.com/_thumbs/0000019/0019232/0019232_0";
		//global $config;
		$ThumDir ="c:\\test";
		$video_id = "12345";
		download_file( $thumb_url, $ThumDir."\\".$video_id);
	
		
?>

Open in new window

0
megarry
Asked:
megarry
  • 2
  • 2
1 Solution
 
gunny051499Commented:
Hi megarry,

no idea what you need this for, but if I got the stuff you want to do right, the attached code should solve your problem.

Cheers

Gunny
<?php
 
// settings
 
$local_count = 1;
$first_source_image = 1;
$last_source_image = 20;
 
$source_prefix = 'http://thumbs.redtube.com/_thumbs/0000019/0019232/0019232_';
$target_prefix = 'xxx-';
$postfix = '.jpg';
 
 
 
/*
 * The & in front of $local_count, causes the variable to be passed in as a reference
 * to the original variable (this is called "call by reference"). When changing it,
 * the variable OUTSIDE the function will be changed, since the local one is just a
 * reference to it!
 */
 
function download_file( $source_prefix, $remote_count, $target_prefix, &$local_count ) {
  global $postfix;
 
  $source = sprintf("%s%03d%s", $source_prefix, $remote_count, $postfix);
  $target = sprintf("%s%s%s", $target_prefix, $local_count, $postfix);
 
  echo "save '" . $source . "' to '" . $target . "' - ";
 
  $rh = @fopen( $source, "rb" );
  if ( $rh === false ) return -1;
  $wh = @fopen( $target, "wb" );
  if ( $wh === false ) return -2;
 
  while ( $buffer = fread( $rh, 4096 ) ) {
    fwrite( $wh, $buffer );
  }
 
  fclose( $wh );
  fclose( $rh );
 
  $local_count++;
 
  return 0;
}
 
 
for ( $remote_count = $first_source_image; $remote_count <= $last_source_image; $remote_count++ ) {
  switch ( download_file( $source_prefix, $remote_count, $target_prefix, $local_count ) ) {
    case "-2":
      echo "unable to write output\n";
      break;
    case "-1":
      echo "unable to read input\n";
      break;
    case "0":
    default:
      echo "transfer succeeded\n";
      break;
  }
}
 
?>

Open in new window

0
 
megarryAuthor Commented:
why use @fopen instead of fopen?  

Sorry, just tryign to learn a but, never seen it used like that before.

0
 
megarryAuthor Commented:
Works great!  I added an if on local_count so it doesn't download more than 10 images from the pool of 20.
0
 
gunny051499Commented:
Hi megarry,

putting an @ in front of any built-in PHP function does effectively disable warning messages, which you would otherwise get, when trying to fopen a non-existing remote file ;-)

It's just a bit cleaner ;-)

Thanks for the points

Gunny
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now