3115

Create square 1:1 thumbnail in PHP

Question:

How can I set this code to return images in 1:1 (square)?

The purpose is to create a square (non stretched) thumbnail.

I've tried making changes in the 'if section'. I get a square image but stretched. I want it to be cropped.

define('THUMBNAIL_IMAGE_MAX_WIDTH', 150); define('THUMBNAIL_IMAGE_MAX_HEIGHT', 150); $source_image_path = {here the source filename}; $thumbnail_image_path = {here de thumb filename}; list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path); switch ($source_image_type) { case IMAGETYPE_GIF: $source_gd_image = imagecreatefromgif($source_image_path); break; case IMAGETYPE_JPEG: $source_gd_image = imagecreatefromjpeg($source_image_path); break; case IMAGETYPE_PNG: $source_gd_image = imagecreatefrompng($source_image_path); break; } $source_aspect_ratio = $source_image_width / $source_image_height; $thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH / THUMBNAIL_IMAGE_MAX_HEIGHT; if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) { $thumbnail_image_width = $source_image_width; $thumbnail_image_height = $source_image_height; } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) { $thumbnail_image_width = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio); $thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT; } else { $thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH; $thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH / $source_aspect_ratio); } $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height); imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height); imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90);

<i>PS. It's not a duplicate, I've read several questions of this topic, but I'm unable to integrate it whit my code.</i>

Answer1:

Use this code this code uploads image to folder and renames the file and thumb will be created with same name

<strong>HTML</strong>

<INPUT NAME="userfile[]" TYPE="file">

image directory "upimg/"<br /> thumb directory thimg

<strong>php processing</strong>

$rename = md5(rand() * time()); $add = "upimg/" . $rename . $_FILES['userfile']['name']; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $add)) { echo "Successfully uploaded the image"; chmod("$add", 0777); } else { exit; } $n_width = 200; $n_height = 200; $tsrc = "thimg/" . $rename . $_FILES['userfile']['name']; if (!($_FILES['userfile']['type'] == "image/jpeg" OR $_FILES['userfile']['type'] == "image/gif")) { exit; } if ($_FILES['userfile']['type'] == "image/gif") { $im = ImageCreateFromGIF($add); $width = ImageSx($im); $height = ImageSy($im); $newimage = imagecreatetruecolor($n_width, $n_height); imageCopyResized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height); if (function_exists("imagegif")) { Header("Content-type: image/gif"); ImageGIF($newimage, $tsrc); } elseif (function_exists("imagejpeg")) { Header("Content-type: image/jpeg"); ImageJPEG($newimage, $tsrc); } chmod("$tsrc", 0777); } if ($_FILES['userfile']['type'] == "image/jpeg") { $im = ImageCreateFromJPEG($add); $width = ImageSx($im); $height = ImageSy($im); $newimage = imagecreatetruecolor($n_width, $n_height); imageCopyResized($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height); ImageJpeg($newimage, $tsrc); chmod("$tsrc", 0777); }

Answer2:

<a href="https://stackoverflow.com/questions/3255773/php-crop-image-to-fix-width-and-height-without-losing-dimension-ratio/3256004#3256004" rel="nofollow">This function</a> did the trick

function crop_img($imgSrc){ //getting the image dimensions list($width, $height) = getimagesize($imgSrc); //saving the image into memory (for manipulation with GD Library) $myImage = imagecreatefromjpeg($imgSrc); // calculating the part of the image to use for thumbnail if ($width > $height) { $y = 0; $x = ($width - $height) / 2; $smallestSide = $height; } else { $x = 0; $y = ($height - $width) / 2; $smallestSide = $width; } // copying the part into thumbnail $thumbSize = min($width,$height); $thumb = imagecreatetruecolor($thumbSize, $thumbSize); imagecopyresampled($thumb, $myImage, 0, 0, $x, $y, $thumbSize, $thumbSize, $smallestSide, $smallestSide); unlink($imgSrc); imagejpeg($thumb,$imgSrc); @imagedestroy($myImage); @imagedestroy($thumb); }

Found in: <a href="https://stackoverflow.com/questions/3255773/php-crop-image-to-fix-width-and-height-without-losing-dimension-ratio/3256004#3256004" rel="nofollow">PHP crop image to fix width and height without losing dimension ratio</a>

Recommend

  • Create square 1:1 thumbnail in PHP
  • Type code in to a text input form, how?
  • Center watermark text on image when unknown size of text
  • SetPixel() too slow to erase image background
  • resizing image function implementation inpage
  • create white image with php
  • Using std::complex with iPhone's vDSP functions
  • dynamic image resize using php
  • python — measuring pixel brightness
  • why imagepng after resize create a black image?
  • PHP - Resized image has larger file size
  • Generated image using PHP and GD is being cut off
  • Having issues with GD thumbnail generator
  • What should I do to not show the legend for all the plots?
  • Display an image after resizing
  • Unexpected behavior of numpy.fft.fft with high precision numbers
  • Zend captcha Image generates blank
  • FFT wrong value?
  • Flip an Image horizontally
  • Casting an array of C structs to a numpy array
  • Mayavi: interpolate face colors in triangular_mesh
  • DOS Batch file to copy certain file types from subdirectories to one folder with rename
  • data.frame rows to list named elements
  • Validating Username And Password from MySQL
  • Suppress comparison always true warning for Macros?
  • How to configure a non-default serviceAccount on a deployment
  • VBA, TRIM part of a Path
  • Muliple upload php script and store in mysql database
  • resize png/jpeg image
  • Get highest value from a file using mSL and mIRC
  • How to create a custom Powershell operator?
  • Julia: How to give multiple workers access to functions that are 'include(…)' into a modul
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?