Jump to content

GD Image Resizing Help Request


Recommended Posts

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.

 

1. 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.

 

2. The detecting of the variables works no problem.

 

3. 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

Link to comment
Share on other sites

i looked the code over, and want to ask....why the work?

 

1) Most people's browser screens should be bigger then 800px

2) Even if they are not, if they open it in a small screen and then maximize the screen (common), it will keep the small versions

3) If JS isn't enabled the site won't work

4) If you really want variable size images, just use CSS with a width: 50% type setup

Link to comment
Share on other sites

Take a look at the main site.  There are still plenty of people out there with an 800x600 resolution.  I set computers up every day and people request that.  Those exact people are our target market as well as the techies.

 

Spectacular Computer Repair

 

When you resize your page to below 800 width, the header images at the top go to 2 lines.  This distorts the page.  I don't want that.  I want the images to resize based upon their current screen width.

 

Most of our target market will not have JavaScript turned off.  Some of the techies might however this header resize is not for them.  They will be able to work around it.  This resizing is for the people that will get confused by it.

 

If there is a better way to do this to resolve two issues, I am all eyes :D.  Can you provide an example what you were speaking about with the CSS?

 

Issues to be resolved

1. When people have their screen width below or = to 800 and

2. When people resize their browser window, the images automatically adjust to compensate.

 

The only way I know to do this is through JavaScript and/or PHP.

 

Wayne

Link to comment
Share on other sites

ok...well...in that case, i still wouldn't use PHP since you are doing the same resize every time.

 

instead, i would use all JS for this. do something like this on window load:

if(bowser <= 800){ //you already know how to test the browser width
  var eles = document.getElementsByTagName('IMG');
  for(var n=0;n < eles.length;n++){
    eles[n].style.width = (eles[n].width / 2) + 'px';
    eles[n].style.height = (eles[n].height / 2) + 'px';
  }
}

you could limit which images get resiszed too by adding a class to each and testing for that

 

Link to comment
Share on other sites

ok...well...how about resizing them once (with GD or just a normal image editor). then, with your JS, instead of resizing them, change the SRC to point to the smaller version.

 

...again...it's just a waste of resources to keep performing the same resize over and over

Link to comment
Share on other sites

I thought about that too however there is other reasons as why I want the GD.  If I only wanted to reduce the images by 800 than that would work however later on I want to increase them as well based upon the resolution of the user's computer.  I don't want to have to create separate images every single time I want to add or subtract or change out header images.  This is why I am doing it through GD.

 

Wayne

Link to comment
Share on other sites

Just trying to bring this thread back to the top to see if I can still get help performing this task.

 

I am at a little loss here.  I have code that works and then when I introduce an if/else statement the same code doesn't work.

Good PHP Code

<?php

$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:
        $im = @imagecreatefromjpeg($file);
        if ($im !== false) { return $im; }

        # GIF:
        $im = @imagecreatefromgif($file);
        if ($im !== false) { return $im; }

        # PNG:
        $im = @imagecreatefrompng($file);
        if ($im !== false) { return $im; }

        # GD File:
        $im = @imagecreatefromgd($file);
        if ($im !== false) { return $im; }

        # GD2 File:
        $im = @imagecreatefromgd2($file);
        if ($im !== false) { return $im; }

        # WBMP:
        $im = @imagecreatefromwbmp($file);
        if ($im !== false) { return $im; }

        # XBM:
        $im = @imagecreatefromxbm($file);
        if ($im !== false) { return $im; }

        # XPM:
        $im = @imagecreatefromxpm($file);
        if ($im !== false) { return $im; }

        # Try and load from string:
        $im = @imagecreatefromstring(file_get_contents($file));
        if ($im !== false) { 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.";
}
?>

 

Same Code with IF/Else statement that doesn't work

<?php

$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:
        $im = @imagecreatefromjpeg($file);
        if ($im !== false) { return $im; }

        # GIF:
        $im = @imagecreatefromgif($file);
        if ($im !== false) { return $im; }

        # PNG:
        $im = @imagecreatefrompng($file);
        if ($im !== false) { return $im; }

        # GD File:
        $im = @imagecreatefromgd($file);
        if ($im !== false) { return $im; }

        # GD2 File:
        $im = @imagecreatefromgd2($file);
        if ($im !== false) { return $im; }

        # WBMP:
        $im = @imagecreatefromwbmp($file);
        if ($im !== false) { return $im; }

        # XBM:
        $im = @imagecreatefromxbm($file);
        if ($im !== false) { return $im; }

        # XPM:
        $im = @imagecreatefromxpm($file);
        if ($im !== false) { return $im; }

        # Try and load from string:
        $im = @imagecreatefromstring(file_get_contents($file));
        if ($im !== false) { 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.
if (1=1) { 
    	  imagepng($image);  // Display the image to the browser
 }
else { 
    	  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.
if (1=1) { 
    	  imagejpeg($image);  // Display the image to the browser
 }
else { 
    	  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.
if (1=1) { 
    	  imagejpeg($image);  // Display the image to the browser
 }
else { 
    	  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.
if (1=1) { 
    	  imagegif($image);  // Display the image to the browser
 }
else { 
    	  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.
if (1=1) { 
    	  imagegd($image);  // Display the image to the browser
 }
else { 
    	  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.
if (1=1) { 
    	  imagegd2($image);  // Display the image to the browser
 }
else { 
    	  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.
if (1=1) { 
    	  imagewbmp($image);  // Display the image to the browser
 }
else { 
    	  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.
if (1=1) { 
    	  imagexbm($image);  // Display the image to the browser
 }
else { 
    	  imagexbm($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.
if (1=1) { 
    	  imagexpm($image);  // Display the image to the browser
 }
else { 
    	  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.";
}
?>

 

All I did was say is if 1=1 then do the same thing and the images don't show up??

 

Wayne

Link to comment
Share on other sites

ok...i'm back...let's see if i try to put my thoughts aside and just help you get your code working :)

 

first...i don't see the resize code any more?

second...you can't say 1=1, if anything you need to say 1 == 1

 

try this code out:

<?php

$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)
$file = $imgp . $imgn; // Combine the pathway and the image name together (ie images/scrfix-logo.png)

header("Content-Type: image/{$ext}");

function resize_image ( $source, $browser_width ) {
  if(!$source) return false;
  if(!$browser_width || $browser_width > 800) return $source;
  $width = imagesx($source);
  $height = imagesy($source);
  $newwidth = $width * 0.5;
  $newheight = $height * 0.5;
  $resize = imagecreatetruecolor($newwidth, $newheight);
  imagecopyresized($resize, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  return $resize;
}

# Open the image
switch($ext) {
  case "png":
    $im = @imagecreatefrompng($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagepng($image);
    break;
  case "jpg":  
  case "jpeg":  
    $im = imagecreatefromjpeg($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagejpeg($image);
    break;
  case "gif":
    $im = @imagecreatefromgif($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagegif($image);
  case "gd":
    $im = @imagecreatefromgd($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagegd($image);
    break;
  case "gd2":
    $im = @imagecreatefromgd2($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagegd2($image);
    break;
  case "wbmp":
    $im = @imagecreatefromwbmp($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagewbmp($image);
    break;
  case "xbm":
    $im = @imagecreatefromxbm($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagexbm($image);
    break;
  case "xpm":
    $im = @imagecreatefromxpm($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagexpm($image);
    break;
}
if(!$img || !$image){
  // Try and load from string:
  if($im = @imagecreatefromstring(file_get_contents($file))){
    if($image = resize_image($im,$width)){
      header("Content-Type: image/jpeg");
      imagejpeg($image);
    }
  }
}
// Destroy
@imagedestroy($img);
@imagedestroy($image);
exit;
?>

Link to comment
Share on other sites

I removed the resample code out of there in an attempt to get the If and else statements to work.

 

Thanks for the tip on the ==.  I am curious as to why that is.  My other programming languages you can utilize either = means is equal to and == means equates to.  What's the difference in PHP.

 

Even if the IF statment was wrong the Else statement should have shown the image the way that I have it written.  It still doesn't show up.

 

I have the choice between the following code

list($width, $height) = getimagesize($image);
$new_width = $width * $percent;
$new_height = $height * $percent;

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

 

Or another one that someone else wrote that is faster.

<?
  // Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.
  // Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled".
  // Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.
  // Author: Tim Eckel - Date: 09/07/07 - Version: 1.1 - Project: FreeRingers.net - Freely distributable - These comments must remain.
  //
  // Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5. Must be greater than zero.
  // Between 0 and 1 = Fast, but mosaic results, closer to 0 increases the mosaic effect.
  // 1 = Up to 350 times faster. Poor results, looks very similar to imagecopyresized.
  // 2 = Up to 95 times faster.  Images appear a little sharp, some prefer this over a quality of 3.
  // 3 = Up to 60 times faster.  Will give high quality smooth results very close to imagecopyresampled, just faster.
  // 4 = Up to 25 times faster.  Almost identical to imagecopyresampled for most images.
  // 5 = No speedup. Just uses imagecopyresampled, no advantage over imagecopyresampled.

function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3) {
  if (empty($src_image) || empty($dst_image) || $quality <= 0) { return false; }
  if ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
    $temp = imagecreatetruecolor ($dst_w * $quality + 1, $dst_h * $quality + 1);
    imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, $src_w, $src_h);
    imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, $dst_h * $quality);
    imagedestroy ($temp);
  } else imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  return true;
}
?>

Link to comment
Share on other sites

i wouldn't bother with the complicated function. if anything, it's saving you fractions of a second. there are other things you can do first that will save you more time (like caching the image)

 

...i would however use the imagecopyresampled:

function resize_image ( $source, $browser_width ) {
  if(!$source) return false;
  if(!$browser_width || $browser_width > 800) return $source;
  $width = imagesx($source);
  $height = imagesy($source);
  $newwidth = $width * 0.5;
  $newheight = $height * 0.5;
  $resize = imagecreatetruecolor($newwidth, $newheight);
  imagecopyresampled($resize, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  return $resize;
}

 

caching the image involves saving the resized version to a new file, and if a request is made later for the same image, same size, return the cached copy instead of recreating it.

Link to comment
Share on other sites

I didn't see the code below.  I will try this code when I get back to the house tonight.

I will post positive or negative results tonight when I have a chance.

 

Thanks,

 

Wayne

 

ok...i'm back...let's see if i try to put my thoughts aside and just help you get your code working :)

 

first...i don't see the resize code any more?

second...you can't say 1=1, if anything you need to say 1 == 1

 

try this code out:

<?php

$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)
$file = $imgp . $imgn; // Combine the pathway and the image name together (ie images/scrfix-logo.png)

header("Content-Type: image/{$ext}");

function resize_image ( $source, $browser_width ) {
  if(!$source) return false;
  if(!$browser_width || $browser_width > 800) return $source;
  $width = imagesx($source);
  $height = imagesy($source);
  $newwidth = $width * 0.5;
  $newheight = $height * 0.5;
  $resize = imagecreatetruecolor($newwidth, $newheight);
  imagecopyresized($resize, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  return $resize;
}

# Open the image
switch($ext) {
  case "png":
    $im = @imagecreatefrompng($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagepng($image);
    break;
  case "jpg":  
  case "jpeg":  
    $im = imagecreatefromjpeg($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagejpeg($image);
    break;
  case "gif":
    $im = @imagecreatefromgif($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagegif($image);
  case "gd":
    $im = @imagecreatefromgd($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagegd($image);
    break;
  case "gd2":
    $im = @imagecreatefromgd2($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagegd2($image);
    break;
  case "wbmp":
    $im = @imagecreatefromwbmp($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagewbmp($image);
    break;
  case "xbm":
    $im = @imagecreatefromxbm($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagexbm($image);
    break;
  case "xpm":
    $im = @imagecreatefromxpm($file);
    $image = resize_image($im,$width);
    if(!$image) break;
    imagexpm($image);
    break;
}
if(!$img || !$image){
  // Try and load from string:
  if($im = @imagecreatefromstring(file_get_contents($file))){
    if($image = resize_image($im,$width)){
      header("Content-Type: image/jpeg");
      imagejpeg($image);
    }
  }
}
// Destroy
@imagedestroy($img);
@imagedestroy($image);
exit;
?>

Link to comment
Share on other sites

That code works great although even with image resampling it still distorts the image.  That's kind of a horrible thing.  I was hoping that it was going to be smooth like I was reading.

 

If I wanted to increase the size of the image because of different width size should I change that if else to a case statement?

 

Wayne

Link to comment
Share on other sites

no...do something like:

 

function resize_image ( $source, $browser_width ) {
  if(!$source || !$browser_width) return false;
  if($browser_width < 800){
    $change = 0.5; //Make it smaller
  }elseif($browser_width < 1000){
    return $source; //No change
  }else{
    $change = 2; //Make it bigger
  }
  $width = imagesx($source);
  $height = imagesy($source);
  $newwidth = $width * $change;
  $newheight = $height * $change;
  $resize = imagecreatetruecolor($newwidth, $newheight);
  imagecopyresized($resize, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
  return $resize;
}

Link to comment
Share on other sites

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.