Jump to content

[SOLVED] image resize script


acidglitter

Recommended Posts

okay i have this script i got off a site a few months ago and it only works 10% of the time...

 

function open_image($file){
// Get extension
$extension = strrchr($file, '.');
$extension = strtolower($extension);
switch($extension){
	case '.jpg':
	case '.jpeg':
		$im = @imagecreatefromjpeg($file);
		break;
	case '.gif':
		$im = @imagecreatefromgif($file);
		break;
	case '.png':
		$im = @imagecreatefrompng($file);
		break;

	// ... etc
	default:
		$im = false;
		break;
}
return $im;
}

// this gets the specifics for each type (i added this)
switch($type){
case 'product':
	$open_image_url = 'images/products';
	$return_url = "newproduct.php?p=2";
	break;
}

 

and then it includes a page that makes the thumbnail image

 

<?php
// Load image
$image = open_image("http://site.com/$open_image_url/$image_name");
if($image == false){die ('Unable to open image');}

// Get original width and height
$width = imagesx($image);
$height = imagesy($image);

// New width and height
$new_width = 180;
$new_height = 180;

// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// The image name
$mini_image=explode('.',$image_name);
$mini_image_name="$mini_image[0]thumbnail.$mini_image[1]";

// Display resized image
header('Content-type: image/jpeg');
imagejpeg($image_resized,"/home/html/$open_image_url/$mini_image_name");
imageDestroy($image_resized); ?>

 

 

 

does anyone see whats wrong?? sometimes it will even say its not a valid jpg file even when it is..

Link to comment
Share on other sites

it just stops at this

if($image == false){die ('Unable to open image');}

 

i don't see why its not working. i've tried different file types and file sizes and files saved by different programs. it just seems like it only randomly works sometimes. and i've also tried showing the url of the image its trying to open and the image shows up fine.

Link to comment
Share on other sites

If the image won't open as the type that its extension suggests, you could try loading it using the functions for the other types, just in case the extension is wrong.

 

You should probably do some kind of sanity check to see that the URL isn't a 404, unless you're 100% certain it's there.

Link to comment
Share on other sites

  • 3 weeks later...
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.