Jump to content

create an image in one php file, load it in another php file.


SirXaph

Recommended Posts

Hi, me again.  :)

I'm actually now creating a new image gallery from scratch using a mixture of ImageMagick and gd, and it's going quite well so far but I've hit a stumbling block with gd and just need to know if what I'm specifically trying to do is just impossible or if there's something else wrong. 

 

Right, the background - I have a php file called resize.php which works like this:

 

check to see if resized image exists

- if not, create it using ImageMagick

---then add the image headers directly, load and display the image using GD

- if so, just add the already pre-existing image directly.

 

The reason I coded it that way was to prevent one of the issues with the other gallery, which was that the thumbnail and web-resized images would effectively have a different filename depending on whether the resized image had already been created (/resized/###.jpg) or whether it needed to be created at display time (resizeimg.php?img=###.jpg).

 

So, the way it works is that resize.php?img=subdir/filename.jpg is the url I always use to get the image, and resize.php?img=subdir/filename.jpg&t=1 gets that image's thumbnail.

examples:

http://antarctica.go-forth.co.uk/p2/resize.php?img=10b/DSCF3337.jpg

http://antarctica.go-forth.co.uk/p2/resize.php?img=dscf1116.jpg&t=1

 

What I'm trying to do now is to obtain these images using a call to imagecreatefromjpeg() in another file:

 

		$src = "resize.php?img=$srcdir_s$image&t=1";
		//echo $src;
		$img=imagecreatefromjpeg($src);
		header('Content-type: image/jpeg');
		header('Content-transfer-encoding: binary');
		imagejpeg($img, null, $resizequal);
		imagedestroy($img);
		exit();		

 

However this code isn't doing anything. 

For test purposes I put that echo $src; in there. uncommented, the script will display the image source name for example resize.php?img=dscf1116.jpg&t=1 - and if I just type in the address bar (after the script's path) loads the image fine - you wind up with the same url I used above.. so it IS a jpeg image.

 

Is it something to do with the fact that while the headers and content of that request will return a jpeg image, the file itself is a php script?

 

The php manual doesn't say that I can't do this but then it doesn't say I can either..

 

I can code around this, just I'd rather not have to because I'd need to duplicate a lot of what resize.php can do in another file, and that just feels messy.

Link to comment
Share on other sites

Personally i use one of these two scripts for resizing images based on what i read i would suggest using the first one:

 

- http://shiftingpixel.com/2008/03/03/smart-image-resizer/

- http://mediumexposure.com/techblog/smart-image-resizing-while-preserving-transparency-php-and-gd-library

 

If you don't want to use existing scripts you can read through the code to see how they tackled the problem

Link to comment
Share on other sites

Using gd alone to resize the images unfortunately doesn't seem to be an option because my host seems to kill* any php script as soon as gd even loads an image that takes up more than about 10mb of ram and I intend to be working with several megapixel digital camera files 'as is'.  :(

Increasing the amount of memory allowed using ini_set doesn't have any impact either because the process itself seems to be terminated regardless of what memory_limit is set to.

*and by kill I mean terminate, end process, etc, so there's not even any errors returned.

I posted about that original issue here: http://www.phpfreaks.com/forums/index.php/topic,249061.0.html

 

So.. is it possible to load a php file as an image from another php file?  ;)

Link to comment
Share on other sites

I think i know what the problem is your host doesn't allow you to change the ini settings nor does it display any errors apparently. Try this:

 

Create a new php file with the following lines:

echo 'first: ' . ini_get('memory_limit');
ini_set('memory_limit', '16MB');
echo 'after: ' . ini_get('memory_limit');

 

if your host doesn't allow you to change ini settings then these values won't change.

Link to comment
Share on other sites

No, I tried that already: http://antarctica.go-forth.co.uk/p2/test1.php

They do change. 

Besides if I was hitting the php memory limit then the call to imagecreatefromjpeg() would fail and I would get an error I can check for, but the whole script shouldn't die and fail to continue after that point.

two scripts - identical but for the size of the image.  I use ini_set to set the memory limit to 80M first.

http://antarctica.go-forth.co.uk/test.php

http://antarctica.go-forth.co.uk/test2.php

 

Link to comment
Share on other sites

Okay forgive the double post but I've tested my issue further.

 

Using the same example file from the last post, http://antarctica.go-forth.co.uk/test2.php is a jpeg file, loaded and displayed using gd.

 

I've made a file called http://antarctica.go-forth.co.uk/test3.php which is identical, but instead of a .jpg image in the call to imagecreatefromjpeg() I actually put test2.php.

<?php
ini_set('memory_limit', '80M');

$im = imagecreatefromjpeg("test2.php");

header('Content-Type: image/jpeg');

imagejpeg($im);

imagedestroy($im);

?>

 

So, it appears I can't load a jpeg created and returned by one php file into another php file that way.

Does anyone know how I would go about doing this?

 

 

Link to comment
Share on other sites

Okay..

Sorry to post again but nobody's replied and I have more info that might help someone answer the question.

I've found out that the best way to do what the test php files were doing is to use code like this:

<?php
ini_set('memory_limit', '80M');

    ob_start();
    include("test2.php");
    $return_str = ob_get_contents();
    ob_end_clean();

header('Content-Type: image/jpeg');

echo($return_str);
?>

Problem with that though is that all I seem able to do with that code is load the image and echo it to the screen.

I kinda wanted to be able to load it as a gd image and do stuff to it, which is why my tests all focus around loading the image using gd functions. 

There doesn't seem to be a way to load an image from a variable rather than from a filename.  I'd rather not go down the route of saving a tempfile, then loading the image from there, unless that's the only way.

Is there a way to do something similar to that code (which I grabbed from the php site) within an imagecreatefromjpeg() function?

 

On an unrelated note, am I right in thinking it's not necessary to specifically set the header as jpeg when you're echoing the contents of a jpeg file (which should already include the header)?

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.