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
https://forums.phpfreaks.com/topic/113120-solved-image-resize-script/
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.

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.

  • 3 weeks later...

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.