Learn It Friday

Things being learned…

Add Images Code

0

http://www.haycroftmedia.com/archives/160/wp-e-commerce-upload-adding-images-to-csv-upload

<?php 
/*VARIABLES*/
$thumbwidth = 96;
$thumbheight = 96;
//If you want to leave your large images as they are, then set $largewidth and $largeheight to 0
$largewidth = 0; 
$largeheight = 0;

/*
NO NEED TO EDIT ANYTHING BELOW HERE
*/

/* FUNCTIONS */
function img_resizer($src,$quality,$w,$h,$saveas) {
	/* v2.5 with auto crop */
	$r=1;
	$e=strtolower(substr($src,strrpos($src,".")+1,3));
	if (($e == "jpg") || ($e == "peg")) {
		$OldImage=ImageCreateFromJpeg($src) or $r=0;
	} elseif ($e == "gif") {
		$OldImage=ImageCreateFromGif($src) or $r=0;
	} elseif ($e == "bmp") {
		$OldImage=ImageCreateFromwbmp($src) or $r=0;
	} elseif ($e == "png") {
		$OldImage=ImageCreateFromPng($src) or $r=0;
	} else {
		_o("Not a Valid Image! (".$e.") -- ".$src);$r=0;
	}
	if ($r) {
		list($width,$height)=getimagesize($src);
		// check if ratios match
		$_ratio=array($width/$height,$w/$h);
		if ($_ratio[0] != $_ratio[1]) { // crop image

			// find the right scale to use
			$_scale=min((float)($width/$w),(float)($height/$h));

			// coords to crop
			$cropX=(float)($width-($_scale*$w));
			$cropY=(float)($height-($_scale*$h));   

			// cropped image size
			$cropW=(float)($width-$cropX);
			$cropH=(float)($height-$cropY);

			$crop=ImageCreateTrueColor($cropW,$cropH);
			// crop the middle part of the image to fit proportions
			ImageCopy(
				$crop,
				$OldImage,
				0,
				0,
				(int)($cropX/2),
				(int)($cropY/2),
				$cropW,
				$cropH
			);
		}

		// do the thumbnail
		$NewThumb=ImageCreateTrueColor($w,$h);
		if (isset($crop)) { // been cropped
			ImageCopyResampled(
				$NewThumb,
				$crop,
				0,
				0,
				0,
				0,
				$w,
				$h,
				$cropW,
				$cropH
			);
			ImageDestroy($crop);
		} else { // ratio match, regular resize
			ImageCopyResampled(
				$NewThumb,
				$OldImage,
				0,
				0,
				0,
				0,
				$w,
				$h,
				$width,
				$height
			);
		}
		_ckdir($saveas);
		ImageJpeg($NewThumb,$saveas,$quality);
		ImageDestroy($NewThumb);
		ImageDestroy($OldImage);
	}
	return $r;
}
//
function dirList ($directory) {

	// create an array to hold directory list
	$results = array();

	// create a handler for the directory
	$handler = opendir($directory);

	// keep going until all files in directory have been read
	while (false !== ($file = readdir($handler))) { 

		// if $file isn't this directory or its parent, 
		// add it to the results array
		if ($file != '.' && $file != '..')
			$results[] = $file;
	}

	// tidy up: close the handler
	closedir($handler);

	// done!
	sort($results);
	return $results;

}

function _ckdir($fn) {
	if (strpos($fn,"/") !== false) {
		$p=substr($fn,0,strrpos($fn,"/"));
		if (!is_dir($p)) {
			_o("Mkdir: ".$p);
			mkdir($p,777,true);
		}
	}
}

/*PROCESS */
set_time_limit ( 0 ) ;
$t1=time();
sleep(50);
$t2=time();
$t_lapsed=$t2-$t1;

$dir = dirList('wp-content/uploads/wpsc/product_images/temp');
$c = 0;
$errors = '';
if ( $dir ) {
	foreach( $dir as $key => $value ){
		$imgsrc = 'wp-content/uploads/wpsc/product_images/temp/' . $value;
		$thumbout = 'wp-content/uploads/wpsc/product_images/thumbnails/'.$value;
		$largeout = 'wp-content/uploads/wpsc/product_images/'.$value;
		//thumb			
		img_resizer($imgsrc,100,$thumbwidth,$thumbheight,$thumbout);

		if ( $largewidth != 0 && $largeheight != 0 ) {			
			//large
			img_resizer($imgsrc,100,$largewidth,$largeheight,$largeout);
		}
	}
	echo '<h1>Images processed</h1>';
} else {
	echo '<h1>No images in temp directory/h1>';
}?>

http://www.designrazzi.com/2013/free-wordpress-themes/

So, what do you think ?