Skip to content

Chris' Laboratory

chrislee.kr – Personal blog as bookshelves

Menu
  • Home
  • Github
  • Contact Me
Menu

To re-size images with keeping aspect ratio and fitting to certain dimensions

Posted on 07/06/201310/09/2016 by Chris Lee
Read Time:1 Minute, 32 Second

To re-size images with keeping aspect ratio and fitting to certain dimensions

function _create_thumbnail($actual_filepath, $folder_path, $target_filename, $width, $height)
{
	// $this->load->library('image_lib');
	
	// $image_lib_config = array(
		  // 'image_library'	=> 'gd2'
		// , 'source_image'	=>	$actual_filepath
		// , 'new_image'		=>	$folder_path.$target_filename
		// , 'maintain_ratio'	=>	true
		// , 'width'			=>	$width
		// , 'height'			=>	$height
 				
	// );
	// $this->image_lib->clear();
	// $this->image_lib->initialize($image_lib_config); 
 			
	// if ( !$this->image_lib->resize() )
	// {
		// return "";
	// }
 			
	// $new_filename = $folder_path.$target_filename;
	// $new_filename = str_replace($_SERVER['DOCUMENT_ROOT'], "", $new_filename);
	// return $new_filename;

	list($source_width, $source_height, $source_type) = getimagesize($actual_filepath);
	switch ($source_type) {
	    case IMAGETYPE_GIF:
	        $source_gdim = imagecreatefromgif($actual_filepath);
	        break;
	    case IMAGETYPE_JPEG:
	        $source_gdim = imagecreatefromjpeg($actual_filepath);
	        break;
	    case IMAGETYPE_PNG:
	        $source_gdim = imagecreatefrompng($actual_filepath);
	        break;
	}

	$source_aspect_ratio = $source_width / $source_height;
	$desired_aspect_ratio = $width / $height;

	if ($source_aspect_ratio > $desired_aspect_ratio) {
	    /*
	     * Triggered when source image is wider
	     */
	    $temp_height = $height;
	    $temp_width = ( int ) ($height * $source_aspect_ratio);
	} else {
	    /*
	     * Triggered otherwise (i.e. source image is similar or taller)
	     */
	    $temp_width = $width;
	    $temp_height = ( int ) ($width / $source_aspect_ratio);
	}

	/*
	 * Resize the image into a temporary GD image
	 */

	$temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
	imagecopyresampled(
	    $temp_gdim,
	    $source_gdim,
	    0, 0,
	    0, 0,
	    $temp_width, $temp_height,
	    $source_width, $source_height
	);

	/*
	 * Copy cropped region from temporary image into the desired GD image
	 */

	$x0 = ($temp_width - $width) / 2;
	$y0 = ($temp_height - $height) / 2;
	$desired_gdim = imagecreatetruecolor($width, $height);
	imagecopy(
	    $desired_gdim,
	    $temp_gdim,
	    0, 0,
	    $x0, $y0,
	    $width, $height
	);
	$new_filename = $folder_path.$target_filename;
	imagejpeg($desired_gdim, $new_filename, 85);
	$new_filename = str_replace($_SERVER['DOCUMENT_ROOT'], "", $new_filename);
	return $new_filename;
}

Reference: http://salman-w.blogspot.com.au/2009/04/crop-to-fit-image-using-aspphp.html

Share

Facebook
Twitter
LinkedIn
Email

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories

  • Databases (11)
    • MongoDB (4)
    • MS-SQL (1)
    • MySQL (6)
  • E-Commerce (8)
    • Magento (8)
  • Finance (2)
  • Frameworks (84)
    • Adobe Flex (1)
    • Angular (ngx) (3)
    • Codeigniter (6)
    • CSS (5)
    • Django (2)
    • Javascript (13)
    • Node.js (6)
    • PHP (17)
    • React Native (4)
    • React.js (1)
    • Sencha Touch (4)
    • Terraform (1)
    • Vue.js (1)
    • WordPress (4)
    • Yii2 (3)
  • General Documents (15)
  • Marketing (3)
  • Mobile Development (33)
    • Android (20)
    • iPhone (13)
  • Platforms (21)
    • Arduino (2)
    • Docker (5)
    • Google App Engine (5)
    • Raspberry Pi (5)
    • Samsung Smart TV (4)
  • Security (17)
  • Server (31)
    • Linux (13)
  • Tools (14)
    • SVN (7)
  • Uncategorized (3)

Search

Recent Posts

  • helm list error stream ID 3; INTERNAL_ERROR
  • Bootstrapping Kubernetes cluster with kubeadm
  • Taint all resources in the one module
  • Alpine – Plugin caching_sha2_password could not be loaded
  • npm link with peerDependencies

Recent Comments

  • Obayed on Binance Auto Trading Bot – Buy low/Sell high with stop loss limit/Trade multiple coins
  • Ari on How to install memcache.so/memcached.so for MAMP Pro (Mac)
  • Mida ali on Binance Auto Trading Bot – Buy low/Sell high with stop loss limit/Trade multiple coins
  • Chris Lee on How to install memcache.so/memcached.so for MAMP Pro (Mac)
  • Chris Lee on Setting Up A VPN Server On OSX 10.6

Tags

1 ajax amazon android android-addpart browser chrislee-kr codeigniter codeigniter-tcpdf com-apple-net-racoon CSS CSS history hack delpaigmail-com entity-addpart-double exception-printing-is-disabled-by-default-for-security-reasons ext-plugins-listpagingplugin ext-plugins-listpagingplugin-example f iphone javascript jquery-defaultchecked jquery-samsung-smart-tv listpagingplugin mac magento-exception-printing-is-disabled-by-default-for-security-reasons magento-sample-data-exception-printing-is-disabled-by-default-for-security-reasons nu-vot null-core-errors-confignotfound-config-mk9engine-ini php samsung-smart-tv-jquery samsung-smart-tv-sdk-ajax samsung-smart-tv-sdk-jquery samsung-tv-sdk samsung-tv-sdk-jquery samsung tv sencha-smart-tv sencha-touch-list-paging smart-tv-jquery sqlite subversion svn tcedook tcpdf-codeigniter uilinebreakmodecharacterwrap-is-deprecated unknown-column-link-area

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2023 Chris' Laboratory | Powered by Minimalist Blog WordPress Theme