Jump to content

Recommended Posts

Simple question, I don't know the answer.

I access the resize script like..

<img src="resize_image.php?file=image.jpg&size=100" />

I want the resize script to be in a function. Thats easy to do, but how would I use it as it wouldn't be accessing it as simple as a page? My resize script is simple, very small and it ends with this

 

header('Content-type: image/jpeg');
imagejpeg($dst,NULL,100);
imagedestroy($src);
imagedestroy($dst);

 

Would it even be possible to put it in afunction actually as it uses a 'header' ?

 

Heres the full resize script.

<?php
$pic = $_GET['file'];
list($width, $height) = getimagesize($pic);

if($width > $height) // so we can square up the crop area
{
$long_s = $height;
$short_s = $height;
}
elseif($width < $height)
{
$long_s = $width;
$short_s = $width;
}
else
{
$long_s = $height;
$short_s = $width;
}

if (!isset($max_width))
    $crop_width = $_GET['size'];
if (!isset($max_height))
    $crop_height = $_GET['size'];

$dim_1=($long_s - $short_s)/2; //top left cords in original to anchor the crop area


$src  = imagecreatefromjpeg($pic);
$dst = imagecreatetruecolor($crop_width, $crop_height);
imagecopyresampled($dst, $src, 0,0,0,$dim_1, $crop_width, $crop_height,$short_s, $short_s );

header('Content-type: image/jpeg');
imagejpeg($dst,NULL,100);
imagedestroy($src);
imagedestroy($dst);
?>

I pretty much put it in a function. Called it via ... imageResize($img['avatar'], '75');

added in the line of code imagejpeg($dst); and removed the header, and it jsut comes up with stuff like...

�w��3׊VNx�Zy�$Ak��� ��s���s��=Ē�������E�@���d+��r~�VA@3��

 

So it needs the header, but if I put the header in, it gives the headers already sent error. Any ideas :/ I need to sort this out.

You need to have this function in its own file called via an img tags src attribute. The reason being is the entire file needs to be treated as an image.

 

I'm not sure you can try placing a call to your function within the src attribute. eg;

 

<img src="<php yourfunction(); ?>" />

 

Might be worth a shot.

So if that wont work, what will? I can't use the function in a seperate file, because i'd need to define the header, which is whats causing the problem I believe. - If I included that into my page, the header would be set for that page so it will not recognise the html, right?

 

No idea :(

You need to have this function in its own file called via an img tags src attribute. The reason being is the entire file needs to be treated as an image.

Sure, the entire file needs to be treated as an image, using the header, putting the function into a seperate file.

The src="..." attribute must contain a URL

So if it MUST contain a url, I can't call the function inside the src attribute, and like I said before.. I can't exactly include the file with the function in my page because it'll set a header upon my page and turn it into image... o.O

This would work fine, but if it only had never returned an headers already sent message.

 

<?php
function thumbnailCreator($pic, $size)
{
header('Content-type: image/jpeg');

list($width, $height) = getimagesize($pic);

if($width > $height) // so we can square up the crop area
{
	$long_s = $height;
	$short_s = $height;
}
elseif($width < $height)
{
	$long_s = $width;
	$short_s = $width;
}
else
{
	$long_s = $height;
	$short_s = $width;
}

if(!isset($max_width)) $crop_width = $size;

if(!isset($max_height)) $crop_height = $size;

$dim_1 = ($long_s - $short_s) / 2; //top left cords in original to anchor the crop area

$dst = imagecreatetruecolor($crop_width, $crop_height);

$src  = imagecreatefromjpeg($pic);

imagecopyresampled($dst, $src, 0, 0, 0, $dim_1, $crop_width, $crop_height,$short_s, $short_s );

imagejpeg($dst,NULL,100);
}
?>

 

Nobody got any ideas?

If I changed that function to save the file, lets say for 75x75. $img['avatar'] holds the image, so if I did.. $img['avatar'] . '-75x75' for a 75x75 size image.

 

So what it would be..

if $img['avatar'] . '-75x75' does not exist.. create it and then display it via img tags easily.. but then the problem would be... is that people can view the source, see the image file as ... myimage-75x75.jpg, and then make an image of a big size and save it as the same sort of file. So then it'll corrupt up where i want the image to display.

 

Any ideas what I can do? I feel like im on the right track. To create a thumbnail and save it first time round, if thumbnail exists, display it via image tags else create thumbnail, and display it via an image tag, but then again what I could do.. is an md5 of the users ID aswell, so even if someone attempted, they'd fail.

avatar holds - profile/photos/img.jpg

 

using like the first resize, changing the NULL to folder name, how do I set the image name as img name, not the directory, just the image, so the location of the new image becomes.. profile/photos/img-54tr9egj39fjwa9rj3-75x75.jpg - the random string is just an example md5 of an id (its not correct, it was just random)

 

I know the NULL changes to 'profile/photos/' I believe or something, but how do I get the current photo name, and add a few things on to it..

 

if I make sense.

						if(file_exists($img['avatar'] . '-' . md5($img['id']) . '-75x75'))
					{
						echo '<td><a href="#"><img src="' . $img['avatar'] . '-' . md5($img['id']) . '-75x75' . '" alt="" border="0" /></a></td>';
					}
					else
					{
						thumbnailCreator($img['avatar'], 75);

						echo '<td><a href="#"><img src="' . $img['avatar'] . '-' . md5($img['id']) . '-75x75' . '" alt="" border="0" /></a></td>';
					}

 

So pretty much what I need, is if I had $img['avatar'] holding 'profile/photos/img.jpg' I need to get 'img' from $img['avatar']

Any idea how?

Well if I can load an inmage without needing to execute resize script, that should work faster than having it run through the resize script, so if I can get the resize script to sav the file like a thumbnail, then it would be faster to display.

 

It's just, I'm not only having one image displayed, I'm having 21, and that slows down my sites performance as it needs to generate it 21 times. So if I can reduce that, only use the script if the file doesn't exist, it'll speed it up alot. Right?

 

Thats what I'm trying to do, with what I gave, I want to be able to get to save the file, but with something on the end of the file name. So if I had file.jpg, it turns to file-whateveriwanthere.jpg...

Nevermind, I'll create my photo galleries right now, when setting a profile photo, to set a thumbnail size of.. 75x75, and a thumbnail size of whatever others. Just run through the process. Then if the file dont exist, just use my resize_image file....

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.