Jump to content

How to change % to pix in script...


StefanRSA

Recommended Posts

I have script that is resizing images on the fly but in % values...

How can I change this script to resize in pix rather than %?

 

The Script:

// Change the content type to jpeg
header('Content-type: image/jpeg');

// Pull the variables from the query string
$fileileSource = $_GET['imagesrc'];
$percent = $_GET['percent'] / 100;
$rotation = $_GET['rotation'];

// Sizes of the image are stored in an array
// index 0 is the width
// index 1 is the height
$size = getimagesize($fileileSource);
$width = $size[0];
$height = $size[1];
$requiredWidth = $width * $percent;
$requiredHeight = $height * $percent;

// pull the image file in to a variable
// for manipulation
$thumbnailCanvas = imagecreatetruecolor($requiredWidth, $requiredHeight
);
$originalCanvas = imagecreatefromjpeg($fileileSource);

// Duplicate and resize (under the bonnet stuff)
imagecopyresized($thumbnailCanvas, $originalCanvas, 0, 0, 0, 0, $requiredWidth, $requiredHeight, $width, $height);

// Rotate the image by the desired degrees and set the created space to white
$final = imagerotate($thumbnailCanvas, $rotation, 16777215);

// Print the image
imagejpeg($final);

Link to comment
https://forums.phpfreaks.com/topic/164257-how-to-change-to-pix-in-script/
Share on other sites

I'm not sure what the question is, if all you want to do is set the image to a specific pixel width and height then you would just not do all of those percentage calculations you're doing below.  It would look more like this:

 

// Change the content type to jpeg
header('Content-type: image/jpeg');

// Pull the variables from the query string
$fileileSource = $_GET['imagesrc'];
$rotation = $_GET['rotation'];

// Sizes of the image are stored in an array
// index 0 is the width
// index 1 is the height
$size = getimagesize($fileileSource);
$width = $size[0];
$height = $size[1];

$requiredWidth = $_GET['width'];
$requiredHeight = $_GET['height'];

// pull the image file in to a variable
// for manipulation
$thumbnailCanvas = imagecreatetruecolor($requiredWidth, $requiredHeight
);
$originalCanvas = imagecreatefromjpeg($fileileSource);

// Duplicate and resize (under the bonnet stuff)
imagecopyresized($thumbnailCanvas, $originalCanvas, 0, 0, 0, 0, $requiredWidth, $requiredHeight, $width, $height);

// Rotate the image by the desired degrees and set the created space to white
$final = imagerotate($thumbnailCanvas, $rotation, 16777215);

// Print the image
imagejpeg($final);

 

The $_POST['width'] and $_POST['height'] variables would contain the actual pixel values you want to pass.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.