TeddyKiller Posted July 5, 2010 Share Posted July 5, 2010 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/ Share on other sites More sharing options...
TeddyKiller Posted July 5, 2010 Author Share Posted July 5, 2010 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��3VNx�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. Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081154 Share on other sites More sharing options...
trq Posted July 5, 2010 Share Posted July 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081158 Share on other sites More sharing options...
PFMaBiSmAd Posted July 5, 2010 Share Posted July 5, 2010 Only if the function returned a URL of the resulting image. The src="..." attribute must contain a URL since the browser fetches that URL and expects an image to be returned at that URL. Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081160 Share on other sites More sharing options...
trq Posted July 5, 2010 Share Posted July 5, 2010 Only if the function returned a URL of the resulting image. The src="..." attribute must contain a URL since the browser fetches that URL and expects an image to be returned at that URL. That's what I figured. Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081162 Share on other sites More sharing options...
TeddyKiller Posted July 5, 2010 Author Share Posted July 5, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081222 Share on other sites More sharing options...
trq Posted July 5, 2010 Share Posted July 5, 2010 Read the first line in my first response. Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081223 Share on other sites More sharing options...
TeddyKiller Posted July 5, 2010 Author Share Posted July 5, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081225 Share on other sites More sharing options...
TeddyKiller Posted July 5, 2010 Author Share Posted July 5, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081235 Share on other sites More sharing options...
TeddyKiller Posted July 5, 2010 Author Share Posted July 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081237 Share on other sites More sharing options...
TeddyKiller Posted July 5, 2010 Author Share Posted July 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081243 Share on other sites More sharing options...
TeddyKiller Posted July 5, 2010 Author Share Posted July 5, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081247 Share on other sites More sharing options...
trq Posted July 5, 2010 Share Posted July 5, 2010 The way you described yourself using the script in your very first post is how it is intended to be used and the only way it really can. I'm really sure sure what the rest of your post has been trying to get at. Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081257 Share on other sites More sharing options...
TeddyKiller Posted July 5, 2010 Author Share Posted July 5, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081265 Share on other sites More sharing options...
TeddyKiller Posted July 5, 2010 Author Share Posted July 5, 2010 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.... Quote Link to comment https://forums.phpfreaks.com/topic/206730-my-resize-script-how-can-i-use-it-in-a-function/#findComment-1081269 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.