Jump to content

Recommended Posts

I have a script that uploads an image using a form. It creates a preview version of image (width of 500px) and a thumbnail version of the image (width of 100px). But I want all of the thumbnails created be the script to be square, but not distorted, so I need to scale the image so that it maximum dimension is 100 (which I can do) but then I need to crop the other dimension to 100 to make it square (which I can't do). mean?

 

Does anyone know a script that I can add to my upload script that will crop the image?

 

Any help is much appreciated, Thanks.

 

This is my upload script if it helps:

$imagename = $_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$target = "Images/Photos/".$imagename;
move_uploaded_file($source, $target);

$imagepath = $imagename;
$save = "Images/Photos/Previews/" . $imagepath; //This is the new file you saving
$file = "Images/Photos/" . $imagepath; //This is the original file

list($width, $height) = getimagesize($file) ; 

$modwidth = 500; 

$diff = $width / $modwidth;

$modheight = $height / $diff; 
$tn = imagecreatetruecolor($modwidth, $modheight) ; 
$image = imagecreatefromjpeg($file) ; 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

imagejpeg($tn, $save, 100) ; 

$save = "Images/Photos/Thumbnails/" . $imagepath; //This is the new file you saving
$file = "Images/Photos/" . $imagepath; //This is the original file

list($width, $height) = getimagesize($file) ; 

$modwidth = 100; 

$diff = $width / $modwidth;

$modheight = $height / $diff; 
$tn = imagecreatetruecolor($modwidth, $modheight) ; 
$image = imagecreatefromjpeg($file) ; 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

imagejpeg($tn, $save, 100) ; 

Link to comment
https://forums.phpfreaks.com/topic/183712-cropping-an-uploaded-image/
Share on other sites

I created this function at some point. It may be of use.

 

<?php

function createThumbnail($src_file, $dst_file, $dst_width, $dst_height, $dst_quality = 75) {
if(!file_exists($src_file)) {
	// Source file does not exist
	return false;
}
if(file_exists($dst_file)) {
	// Destination file already exists
	return false;
}
if(!is_writable(dirname($dst_file))) {
	// Destinaton directory is not writable
	return false;
}

list($src_width, $src_height, $src_type) = getimagesize($src_file);

switch($src_type) {
	case IMAGETYPE_GIF:
		$src_image = imagecreatefromgif($src_file);
		break;
	case IMAGETYPE_JPEG:
		$src_image = imagecreatefromjpeg($src_file);
		break;
	case IMAGETYPE_PNG:
		$src_image = imagecreatefrompng($src_file);
		break;
	default:
		// Unsupported image type
		return false;
}

$dst_image = imagecreatetruecolor($dst_width, $dst_height);

if($src_width > $src_height) {
	$x_offset = floor( ( $src_width - $src_height ) / 2 );
	imagecopyresampled($dst_image, $src_image, 0, 0, $x_offset, 0, $dst_width, $dst_height, $src_height, $src_height);
}
elseif($src_width < $src_height) {
	$y_offset = floor( ( $src_height - $src_width ) / 2 );
	imagecopyresampled($dst_image, $src_image, 0, 0, 0, $y_offset, $dst_width, $dst_height, $src_width, $src_width);
}
else {
	imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height);
}

imagejpeg($dst_image, $dst_file, $dst_quality);

imagedestroy($src_image);
imagedestroy($dst_image);

return true;
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.