Jump to content

Recommended Posts

I have written a php script to dynamically resize an image an covert it's file type to jpeg.

 

<?php
//include_once('ezuploader.php');
  header('Content-Type: image/jpeg');
//get stuff
  $image = $_GET['image'];
  $size = $_GET['size'];
  $quality = $_GET['quality'];

//make thumbnail method
  function makeThumb($image,$max_size,$quality){
if(file_exists($image)) {
//get image extension
  $parts = explode('.',$image);
  $ext = $parts[1];
//get the width and height of origonal image
  list($ow, $oh) = getimagesize($image);
//check if image is larger the $max_size
  if($ow > $max_size || $oh > $max_size) {
  //image dim(s) are larger therfore resize
	$ratio = $ow/$oh;
  //if width > height
	if($ow > $oh) {
	  $nw = $max_size;
	  $nh = $nw/$ratio;
	}
  //hegith > width
	else if($oh > $ow) {
	  $nh = $max_size;
	  $nw = $nh * $ratio;
	}
  //width and height are equal
	else {
	  $oh = $max_size;
	  $nh = $max_size;
	}
  }
//the image is already an appropriate size
  else {
	$nw = $ow;
	$nh = $oh;
  }//end of image size check
//decide which type of image is being sesized and convert to .jpeg
switch($ext) {
  case "jpg":
	$new_image = imagecreatefromjpeg($image);
  break;
  case "jpeg":
	$new_image = imagecreatefromjpeg($image);
  break;
  case "png":
	$new_image = imagecreatefrompng($image);
  break;
  case "gif":
	$new_image = imagecreatefromgif($image);
  break;
}
// create blank image with new width and height
  $blank_image = imagecreatetruecolor($nw, $nh);
//copy old image into new image with new dimensions
  imagecopyresampled($blank_image, $new_image, 0, 0, 0, 0, $nw, $nh ,$ow ,$oh);
// Output
  imagejpeg($blank_image,NULL,$quality);
//free memory
  imagedestroy($blank_image);
}
else {
  echo 'no files exists dumby...';
}
  }//end
//make thumb
  makeThumb($image,$size,$quality);
?>

 

This is how I intend to use it...

 

<p><img src="resizer.php?image=image_to_be_resized.png&quality=75&size=150"/></p>

 

However I get this error when I run it....

 

The image “http://mysite.com/resizer.php?image=localhost/wb/modules/adbaker/images/image_to_be_resized.png&quality=75&size=150” cannot be displayed because it contains errors.

 

I worked when I was just passing jpeg files through and I see no reason why the conversion shouldn't work.  any help as always is much appreciated.

 

Mike.

Link to comment
https://forums.phpfreaks.com/topic/234008-resize-image-and-convert-its-file-type/
Share on other sites

Still having issues even with the switch for differnt mime types.  The crazy thing is if i print out the $header and $mime variables to the screen they are correct for the type of image being resized.

 

Here is the code again with the changes I have made....

<?php
//get stuff
  $image = $_GET['image'];
  $size = $_GET['size'];
  $quality = $_GET['quality'];
//make thumbnail method
  function makeThumb($image,$max_size,$quality){
  //get image info
list($ow, $oh, $type) = getimagesize($image);
$mime = image_type_to_mime_type($type);
  //output header based on image type
switch($mime) {
  case "image/jpg":
	$header = 'Content-Type: image/jpeg';
  break;
  case "image/jpeg":
	$header = 'Content-Type: image/jpeg';
  break;
  case "image/png":
	$header = 'Content-Type: image/png';
  break;
  case "image/gif":
	$header = 'Content-Type: image/gif';
  break;
}
header($header);
  //check if image is larger the $max_size
if($ow > $max_size || $oh > $max_size) {
//image dim(s) are larger therfore resize
  $ratio = $ow/$oh;
//if width > height
  if($ow > $oh) {
	$nw = $max_size;
	$nh = $nw/$ratio;
  }
//hegith > width
  else if($oh > $ow) {
	$nh = $max_size;
	$nw = $nh * $ratio;
  }
//width and height are equal
  else {
	$oh = $max_size;
	$nh = $max_size;
  }
}
  //the image is already an appropriate size
else {
  $nw = $ow;
  $nh = $oh;
}//end of image size check
  //decide which type of image is being sesized and convert to .jpeg
switch($mime) {
  case "image/jpg":
	$new_image = imagecreatefromjpeg($image);
  break;
  case "image/jpeg":
	$new_image = imagecreatefromjpeg($image);
  break;
  case "image/png":
	$new_image = imagecreatefrompng($image);
  break;
  case "image/gif":
	$new_image = imagecreatefromgif($image);
  break;
}
  // create blank image with new width and height
$blank_image = imagecreatetruecolor($nw, $nh);
  //copy old image into new image with new dimensions
imagecopyresampled($blank_image, $new_image, 0, 0, 0, 0, $nw, $nh ,$ow ,$oh);
  // Output resized image
switch($mime) {
  case "image/jpg":
	imagejpeg($blank_image,NULL,$quality);
  break;
  case "image/jpeg":
	imagejpeg($blank_image,NULL,$quality);
  break;
  case "image/png":
	imagepng($blank_image);
  break;
  case "image/gif":
	imagegif($blank_image);
  break;
}
  //free memory
imagedestroy($blank_image);
  }//end
//make thumb
  makeThumb($image,$size,$quality);
?>

If your goal was to convert the images to jpeg, you didn't need to make those changes to your code. You were using imagejpeg() to output the image, along with a matching  'Content-Type: image/jpeg' header.

 

You need to troubleshoot what your code is doing.

 

What is an actual URL (hide your actual domain name) you are using, because the error message implies you are supplying the following path to the image localhost/wb/modules/adbaker/images/image_to_be_resized.png and that is likely invalid unless you have created a folder named localhost/wb/....... relative to the page where the resizer.php script is located.

The resize script is located in the same root directory the images folder is ie mysite.com/resizer.php and mysite.com/images.  I have changed the path the be a relative one ie resizer.php?image=images/image.png.

 

With this script I posted above both pngs and jpgs work fine the only issue is with gif images.  I have added a check to see if the image I am trying to resize exists and that returns true like it should I just get the "The image “images/image.gif” cannot be displayed because it contains errors" error.

 

Mike.

What GD Version and/or is GIF Read Support present?

 

var_dump(gd_info());

 

Here is my GD info

GD Support enabled

GD Version              bundled (2.0.34 compatible)

FreeType Support enabled

FreeType Linkage with freetype

FreeType Version 2.3.9

GIF Read Support enabled

GIF Create Support enabled

JPEG Support         enabled

libJPEG                    Version 6b

PNG Support         enabled

libPNG Version         1.2.37

WBMP Support         enabled

XBM Support         enabled

 

I thought it might have something to do with my php settings but things look ok to me.  I will try out the link suggested for phpThumb.  In the end though I would prefer to write my own.

You do have GIF support. So, you need to debug why your code is failing to operate on a gif image. Perform the following steps -

 

1) Add the following two lines of php code, immediately after the first <?php tag in your resizer.php code -

 

ini_set("display_errors", "1");
error_reporting(-1);

 

2) Comment out the Content-type: header() statement, so that you will see any php error messages and the raw image data.

 

3) Browse directly to the resizer.php?.... URL that you are putting into the <img src="..." attribute so that you can see the result of any output from the code.

 

Are there any php errors reported?

 

Also, posting your current code for resizer.php would help.

I found the error.  Would you believe it was just a typo?!?  thanks for everyone's help on this.  Here is the fully working code for others to make use of.

 

<?php
//get stuff
  $image = $_GET['image'];
  $max_size = $_GET['size'];
  $quality = $_GET['quality'];
//
if(file_exists($image)) {
//get image info
  list($ow, $oh, $type) = getimagesize($image);
  $mime = image_type_to_mime_type($type);
//output header based on image type
  switch($mime) {
case "image/jpg":
  $header = 'Content-Type: image/jpeg';
break;
case "image/jpeg":
  $header = 'Content-Type: image/jpeg';
break;
case "image/png":
  $header = 'Content-Type: image/png';
break;
case "image/gif":
  $header = 'Content-Type: image/gif';
break;
  }
  //header($header);
//check if image is larger the $max_size
  if($ow > $max_size || $oh > $max_size) {
  //image dim(s) are larger therfore resize
$ratio = $ow/$oh;
  //if width > height
if($ow > $oh) {
  $nw = $max_size;
  $nh = $nw/$ratio;
}
  //hegith > width
else if($oh > $ow) {
  $nh = $max_size;
  $nw = $nh * $ratio;
}
  //width and height are equal
else {
  $nw = $max_size;
  $nh = $max_size;
}
  }
//the image is already an appropriate size
  else {
$nw = $ow;
$nh = $oh;
  }//end of image size check
//decide which type of image is being sesized and convert to .jpeg
  switch($mime) {
case "image/jpg":
  $new_image = imagecreatefromjpeg($image);
break;
case "image/jpeg":
  $new_image = imagecreatefromjpeg($image);
break;
case "image/png":
  $new_image = imagecreatefrompng($image);
break;
case "image/gif":
  $new_image = imagecreatefromgif($image);
break;
  }
// create blank image with new width and height
  $blank_image = imagecreatetruecolor($nw, $nh);
//copy old image into new image with new dimensions
  imagecopyresampled($blank_image, $new_image, 0, 0, 0, 0, $nw, $nh ,$ow ,$oh);
// Output resized image
  switch($mime) {
case "image/jpg":
  imagejpeg($blank_image,NULL,$quality);
break;
case "image/jpeg":
  imagejpeg($blank_image,NULL,$quality);
break;
case "image/png":
  imagepng($blank_image);
break;
case "image/gif":
  imagegif($blank_image);
break;
  }
//free memory
  imagedestroy($blank_image);
}
else {
  echo "<p>File does not exist...</p>";
}
?>

 

Thanks again.

 

Mike.

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.