0

I have the following scenario. I have about around 600 pictures. Most of theme have in bottom a logo which should be cut it out because branding issue, but not all of theme.(with ftp and photoshop would be a challange)The approach what I was thinking about I list all of my images form folders and I add to theme a hyperlink which should fire on a method class which cuts out 57px from bottom, because the image listing limited to height and width, after cutting should not be present on the page

the url looks like

cut.php?target=http://example.com/hideit/2012/03/myimage.jpg

I want to reset the get parameter after execution to avoid problems on page refresh while would cut again from that image the defined pixels. I was trying the following

function cutAndsave($jpg){              
    $folder = explode('/', $jpg);
    $path = 'I:\\xampp\\htdocs\\hideit\\'. $folder[4]. '\\'. $folder[5] .'\\'.$folder[6] ;      

    list($width, $height) = getimagesize($jpg);     
    $offset_x = 0;
    $offset_y = 0;      
    $new_height = $height - 57;
    $new_width = $width;        
    $image = imagecreatefromjpeg($jpg);
    $new_image = imagecreatetruecolor($new_width, $new_height);
    imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);             
    header('Content-Type: image/jpeg');     
    imagejpeg($new_image,$path, 90);
    header("Location: /cat.php/");
    die();      
    }

but the last header call won't work in my case

fefe
  • 8,755
  • 27
  • 104
  • 180
  • This sounds like the wrong approach. What is this supposed to do? Display the image for a split second, then redirect? If you want to avoid repeating the same operation, *cache* the cropped image server-side. – deceze Feb 15 '13 at 08:38
  • I know is not the best workaround but it has to happen fast as possible. I list images from a given folder than I choose which one should be cropped that's why I'm using get variable – fefe Feb 15 '13 at 08:43
  • But it makes no sense to redirect here! The response to a request for a URL is either a redirect or the requested content. Doing both doesn't make sense. Wanting to "reset the GET parameters" doesn't make sense. A URL is a URL and a response to it is a response. There's nothing else. – deceze Feb 15 '13 at 08:45
  • 1
    Your concrete problem is that you're sending output, see http://stackoverflow.com/questions/8028957/headers-already-sent-by-php. Your bigger problem is that what you're trying to do is wrong. – deceze Feb 15 '13 at 08:46
  • Okay I'm lost and how should I get closer to my approach – fefe Feb 15 '13 at 08:51
  • What exactly is your concern/goal in the first place? – deceze Feb 15 '13 at 09:36
  • I'm going to extend my topic – fefe Feb 15 '13 at 09:48

2 Answers2

1

Your concrete problem at hand is that you're trying to send an HTTP header after you output an image. HTTP headers can only be sent before any content; they're headers after all.

The bigger problem is that the idea is nonsense.

The client requests a URL, example.com/image.php?id=42.jpg. You may think of this URL as a filename. The image is identified by the filename/URL. Different URL, different image. It also doesn't matter that this is a PHP script and not a physical file on a hard disk somewhere, that detail is irrelevant to the client. The client requests the URL and receives an image in return, that's all that matters. Whether the image is just read as is from a data storage, is resized by a script on the fly, is live drawn by unicorns behind the scenes, that all doesn't matter; it's an implementation detail.

URL request, response. That's the important concept you need to grok for working with web servers.

As such, "resetting URL parameters" is pointless. You can't do that. You can redirect the client to a different URL, but that's a different file/URL then. If you want the client to get the image, you respond with an image whenever the client asks for the URL. If the user refreshes the page so the client requests the URL again, so be it.

If you do not want to do the image cutting again and again, cache the resized file. In code, check:

  1. Oh, somebody's requesting image #42.
  2. Do I have a resized version of image #42 already?
    • If no, create a resized version and save it somewhere.
  3. Serve the resized image #42.

On top of that you can set HTTP headers that influence the client's caching behavior, so the client will keep the image in its cache and not request it again every time.


In case it's also as simple as not overwriting the original. Always keep the original intact, the cut version should be a copy of it. You can either generate that copy as described above, or when the original is uploaded. But don't change the behavior of a URL depending on whether you have already processed the image or not.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

You have an error in line header("Location: /cat.php/");

It should be

header("Location: /cut.php");

if you just want to go back to last page.

If you want to additionally show your image you can do

header("Location: /cut.php?show=$path")

and then do a script in your file which prints an <img> tag.

Klaus F.
  • 135
  • 14