PHP 自动调整大小/裁剪图像 - CI

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 自动调整大小/裁剪图像 - CI相关的知识,希望对你有一定的参考价值。

// Store uploaded file data
$data = array('userfile' => $this->upload->data());
// Set source file path
$source_file = $data['userfile']['file_path'].$data['userfile']['file_name'];
// Set resized file name
$resized_file = $data['userfile']['file_path'].'sm_'.$data['userfile']['file_name'];
// Set cropped file name
$cropped_file = $data['userfile']['file_path'].'thumb_'.$data['userfile']['file_name'];

// Load in the image manipulation library
$this->load->library('image_lib');

// Determine final width and height
$wished_output_width = 160;
$wished_output_height = 130;

// Parameters for file resize
$config['source_image'] = $source_file;
$config['new_image'] = $resized_file;
$config['image_library'] = 'gd2';
$config['create_thumb'] = false;

list($width, $height, $type, $attr) = getimagesize($source_file);

// Check to see if we want a square image
if($wished_output_width == $wished_output_height)
{
	if($width > $height) {
	    $san = $width / $height;
	    $san2 = $height / $wished_output_height;
	    $new_width = round($width / $san2);
	    $new_height = round($new_width / $san);
	}
	elseif($width < $height) 
	{
	    $san = $height / $width;
	    $san2 = $width / $wished_output_width;
	    $new_height = round($height / $san2);
	    $new_width = round($new_height / $san);
	} 
	else 
	{
		$new_height = $wished_output_height;
		$new_width = $wished_output_width;
	}
}
else
{
	// If it's not square
	// run some numbers to determine the right dimensions
	if($width > $height) {
	    $san = $width / $height;
	    $san2 = $height / $wished_output_height;
	    $new_width = round($width / $san2);
	    $new_height = round($new_width / $san);
	}
	elseif($width < $height) 
	{
	    $san = $height / $width;
	    $san2 = $width / $wished_output_width;
	    $new_height = round($height / $san2);
	    $new_width = round($new_height / $san);
	} 
	else 
	{
		// If someone uploads a square image
		if($wished_output_width > $wished_output_height)
		{
			$san = $height / $width;
		    $san2 = $width / $wished_output_width;
		    $new_height = round($height / $san2);
		    $new_width = round($new_height / $san);
		}
		elseif($wished_output_width < $wished_output_height)
		{
			$san = $width / $height;
	    	$san2 = $height / $wished_output_height;
	    	$new_width = round($width / $san2);
	    	$new_height = round($new_width / $san);
		}
		else
		{
			// ...be completely baffled...
		}
	}
}

// ...more parameters for file resize
$config['width'] = $new_width;
$config['height'] = $new_height;

// Initiate file resize
$this->image_lib->initialize($config);
$this->image_lib->resize();

// Determine if file is already the right size
if($new_width == $wished_output_width && $new_height == $wished_output_height) {
    // Do nothing because it's already the right dimensions
}else {
	// Determine where to set crop offset for x and y
    $x = (($new_width - $wished_output_width) > 0) ? ($new_width - $wished_output_width)/2 : 0;
    $y = (($new_height - $wished_output_height) > 0) ? ($new_height - $wished_output_height)/2 : 0;
    
    // Parameters for file cropping
    $config['width'] = $wished_output_width;
    $config['height'] = $wished_output_height;
    $config['x_axis'] = $x;
    $config['y_axis'] = $y;
    $config['maintain_ratio'] = FALSE;
    $config['source_image'] = $resized_file;
    $config['new_image'] = $cropped_file;
    
    // Initiate file cropping
    $this->image_lib->initialize($config);
    $this->image_lib->crop();
}

以上是关于PHP 自动调整大小/裁剪图像 - CI的主要内容,如果未能解决你的问题,请参考以下文章

PHP:图像调整大小和裁剪为纵向

php调整图像大小然后裁剪图像问题

PHP图像调整大小和裁剪功能

Dart/Flutter 替代 PHP 调整裁剪图像大小?

php 裁剪图像并将图像调整为自定义大小

使用 PHP 图像调整大小/裁剪内置方法的高内存使用率