Table of Contents

jdPhpLib

jdPhpLib is my library of various useful functions I've written in PHP. Because these functions vary wildly, I do not store them in a single PHP file, but instead just copy and paste the functions I need for a particular job.

jdResize

This function returns the adjusted width and height of an image if it were resized to a maximum of $width by $height. The function keeps the aspect ratio the same and returns an array that contains the new width, height, and a string that can be inserted directly into an HTML image tag.

jdResize ($filename, $width, $height);
////////////////////
// jdResize
//////////
// Get the size of an image, calculate a new width and height based on
// the width and height specified (while maintaining aspect ratio) and 
// return a width and height attribute that can be attached to an img tag.
//////////
function jdResize($image, $width, $height) {

	// Get the image size
	$size = getimagesize($image);
	
	// If there was a problem reading the image size
	if (!$size) {
		return false;
	}
	
	// Set some more readable variables
	$originalWidth = $size[0];
	$originalHeight = $size[1];
	
	// Figure out if the image is portrait or landscape, set the longest
	// side, then calculate the short side.
	if ($originalWidth < $originalHeight) // Portrait
	{

		// Set the longest side (height)
		$newHeight = $height;
		
		// Calculate the aspect ratio
		$aspect = $originalWidth / $originalHeight;
					
		// Calculate the new pixels for the short side (width)
		$newWidth = round($newHeight * $aspect);
		
	} 
	else // Landscape
	{
	
		// Set the longest side (width)
		$newWidth = $width;
		
		// Calculate the aspect ratio
		$aspect = $originalHeight / $originalWidth;
					
		// Calculate the new pixels for the short side (height)
		$newHeight = round($newWidth * $aspect);
		
	}
	
	// Setup an array to return
	$retArray[0] = $newWidth;
	$retArray[1] = $newHeight;
	$retArray[2] = "width=\"$newWidth\" height=\"$newHeight\" debug=\"$originalWidth x $originalHeight : $aspect\"";
	
	// Return the array
	return $retArray;

}

jdCurl

This function does a simple CURL post.

jdCurl ($url, $fields);
/**
 * Simplified curl call.
 *
 * @params $url
 * @params $fields
 */
function jdCurl($url, $fields, $head = FALSE) {
	// Initialize curl
	$ch = curl_init();
	// Set the curl options
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_POST,2);
	curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
	curl_setopt($ch,CURLOPT_HEADER, TRUE);
	// Get the response
	$response = curl_exec($ch);
	// Split the header and body parts
	$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
	$header = substr($response, 0, $header_size);
	$body = substr($response, $header_size);
	// Close curl
	curl_close($ch);
	// If the header option was set to true
	if ($head === TRUE) {
		// Return the header and body
		return array(
			'header' => trim($header),
			'body' => $body
		);
	} else {
		// Return the body
		return $result;
	}
}
comments powered by Disqus
php/jdphplib.txt · Last modified: 2020/06/01 22:53 (external edit)