Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php function crop image

$image = imagecreatefromjpeg($_GET['src']);
$filename = 'images/cropped_whatever.jpg';

$thumb_width = 200;
$thumb_height = 150;

$width = imagesx($image);
$height = imagesy($image);

$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;

if ( $original_aspect >= $thumb_aspect )
{
   // If image is wider than thumbnail (in aspect ratio sense)
   $new_height = $thumb_height;
   $new_width = $width / ($height / $thumb_height);
}
else
{
   // If the thumbnail is wider than the image
   $new_width = $thumb_width;
   $new_height = $height / ($width / $thumb_width);
}

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

// Resize and crop
imagecopyresampled($thumb,
                   $image,
                   0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
                   0 - ($new_height - $thumb_height) / 2, // Center the image vertically
                   0, 0,
                   $new_width, $new_height,
                   $width, $height);
imagejpeg($thumb, $filename, 80);
Comment

PREVIOUS NEXT
Code Example
Php :: where with and and or in a laravel 
Php :: replace text in string php 
Php :: php concat objects 
Php :: how to build laravel database 
Php :: php api method post 
Php :: pass image path in laravel blade 
Php :: count_chars (PHP 4, PHP 5, PHP 7, PHP 8) count_chars — Return information about characters used in a string 
Php :: laravel activity log package 
Php :: session not working php 
Php :: wordpress reserved image size names 
Php :: php sodium extension xampp 
Php :: check what kind of file was uploaded php 
Php :: count with condition laravel 
Php :: include in php 
Php :: how to make custom logiger in laravel 
Php :: SoapClient Laravel 8 
Php :: excel date format in php 
Php :: how get data if has relation in laravel 
Php :: wordpress admin redirecting to http 
Php :: php add variable to array 
Php :: select option in laravel 
Php :: a php session was created by a session_start() 
Php :: Installing Maatwebsite excel import export package 
Php :: how to sort with array and after print by for loop in php 
Php :: como destruir uma variavel de sessão 
Php :: php url parameters 
Php :: php check image size before upload 
Php :: php number format without rounding 
Php :: laravel e commerce full project 
Php :: url segment laravel 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =