GD Image resizing seems to be pretty straight forward however I have managed to screw this up and need some help.
I believe it is the If and Else statements that are screwing it up.
Here is my scenario.
I am detecting user screen width in JavaScript and sending the information via a variable back to php when calling the images. The Javascript I wrote works no problem.
The detecting of the variables works no problem.
The initial displaying images to the screen without the If/Else statements works no problem. There must be something screwed up in my PHP If/Else statements but I am not sure what.
Spectacular Computer Repair OR http://67.199.62.141/test4.php
Below, you will find 2 IF/Else statements. The one for JPG I was working on in an attempt to get this working. The others do not have the resize part in it. I merely wanted the images to show up first with the statements before I resized them.
PHP Code
<?php
$percent = 0.5; // Change this to reduce images by percentange based upon an 800 pixel width or below.
$imgp = $_GET["imgpath"]; // The image pathway returned from JavaScript. (ie. images/)
$width = $_GET["width"]; // The users screen width returned from JavaScript? (ie 800)
$imgn = $_GET["imgname"]; // The image name returned from JavaScript. (ie scrfix-logo.png)
$ext = substr($imgn, strrpos($imgn, '.') + 1); // Find the extension from the image name (ie jpg)
$image = $imgp . $imgn; // Combine the pathway and the image name together (ie images/scrfix-logo.png)
header("Content-Type: image/" . $ext); // What type of image are we working with?
//Test to see what type of file is loaded. Even if an incorrect extension is loaded, load the image.
function open_image($file) {
# JPEG:
$img = @imagecreatefromjpeg($file); //Test to see if we can find the image
if ($img !== false)
{
if($width=<800)
{
// Get new sizes
list($width, $height) = getimagesize($file);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$resize = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($file);
// Resize
imagecopyresized($resize, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
break;
}
else { // Do Nothing }
return $resize;
}
# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false)
{
if($width=<800)
{
// Get new sizes
list($width, $height) = getimagesize($im);
$newwidth = $width * $percent;
$newheight = $height * $percent;
break;
}
else { // Do Nothing }
return $im;
}
# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false)
{
if($width=<800)
{
// Get new sizes
list($width, $height) = getimagesize($im);
$newwidth = $width * $percent;
$newheight = $height * $percent;
break;
}
else { // Do Nothing }
return $im;
}
# GD File:
$im = @imagecreatefromgd($file);
if ($im !== false)
{
if($width=<800)
{
// Get new sizes
list($width, $height) = getimagesize($im);
$newwidth = $width * $percent;
$newheight = $height * $percent;
break;
}
else { // Do Nothing }
return $im;
}
# GD2 File:
$im = @imagecreatefromgd2($file);
if ($im !== false)
{
if($width=<800)
{
// Get new sizes
list($width, $height) = getimagesize($im);
$newwidth = $width * $percent;
$newheight = $height * $percent;
break;
}
else { // Do Nothing }
return $im;
}
# WBMP:
$im = @imagecreatefromwbmp($file);
if ($im !== false)
{
if($width=<800)
{
// Get new sizes
list($width, $height) = getimagesize($im);
$newwidth = $width * $percent;
$newheight = $height * $percent;
break;
}
else { // Do Nothing }
return $im;
}
# XBM:
$im = @imagecreatefromxbm($file);
if ($im !== false)
{
if($width=<800)
{
// Get new sizes
list($width, $height) = getimagesize($im);
$newwidth = $width * $percent;
$newheight = $height * $percent;
break;
}
else { // Do Nothing }
return $im;
}
# XPM:
$im = @imagecreatefromxpm($file);
if ($im !== false)
{
if($width=<800)
{
// Get new sizes
list($width, $height) = getimagesize($im);
$newwidth = $width * $percent;
$newheight = $height * $percent;
break;
}
else { // Do Nothing }
return $im;
}
# Try and load from string:
$im = @imagecreatefromstring(file_get_contents($file));
if ($im !== false)
{
if($width=<800)
{
// Get new sizes
list($width, $height) = getimagesize($im);
$newwidth = $width * $percent;
$newheight = $height * $percent;
break;
}
else { // Do Nothing }
return $im;
}
return false;
}
// Test what extension we are working with and display the image.
switch($ext) {
case "png":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagepng($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "jpg":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagejpeg($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "jpeg":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagejpeg($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "gif":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagegif($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "gd":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagegd($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "gd2":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagegd2($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "wbmp":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagewbmp($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "xbm":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagepng($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
case "xpm":
$image = open_image($image); // Image name from JavaScript passed to the open_image function.
imagexpm($image); // Display the image to the browser
//Check to see if we can find the image
if ($image === false) {
die ('Unable to find image');
}
imagedestroy($image);
break;
default:
echo "Unable to find any images.";
}
?>
Thanks for any insight.
Wayne