leo Posted November 11, 2012 Share Posted November 11, 2012 Ok, im new to php and trying to develop an app that allows me to upload image then resize them only on the output, i managed to save the image on the server in a folder, now i want to be able to display this images on the site in different sizes depending on what part of the site. like thumbnails and a bigger scale ect... so far i have this code $image_properties = getimagesize("uploads/orders.jpg"); $image_width = $image_properties[0]; $image_height = $image_properties[1]; $image_ratio = $image_width / $image_height; echo"<img src=\"$image_ratio\">"; here i trying to divide the image by half... i no im a long way off i thought this would work due to retrieving the image size properties then dividing it by half, then echoing it out in an image tag, haha.... eventually i would like to master this and be able to change the size by assigning numbers to variable, keeping the size proportions. help on this topic would be great, i serious need it breaking down, am i far off? Kind Regards Link to comment Share on other sites More sharing options...
margaux Posted November 11, 2012 Share Posted November 11, 2012 You could do the division on the image size first and then set the width and height attributes on the img tag $image_properties = getimagesize("uploads/orders.jpg"); $image_width = $image_properties[0]/2; $image_height = $image_properties[1]/2; echo "<img src=\"uploads/orders.jpg\" width=\"$image_width\" height=\"image_height\">"; 1 Link to comment Share on other sites More sharing options...
leo Posted November 12, 2012 Author Share Posted November 12, 2012 thank you, i will have a play Link to comment Share on other sites More sharing options...
Paul Posted November 12, 2012 Share Posted November 12, 2012 Hi leo and margaux, Please correct me if I'm wrong but what you are doing here doesn't actually resize the image from the point of view of a browser downloading it...does it? If leo uses the width and height variables to change the image attributes in the img tag then the browser will still need to download the full size image. I'm trying to do a similiar thing and have just started experiementing with the functions imagecopyresampled() and imagecopyresized(). Hope this helps. Cheers Paul Link to comment Share on other sites More sharing options...
Larry Posted November 12, 2012 Share Posted November 12, 2012 Yes, that's correct. The browser still downloads the full image. Link to comment Share on other sites More sharing options...
Paul Posted November 12, 2012 Share Posted November 12, 2012 Leo, Have a look at the following code that I'm using to resample an image that a user is uploading. It appears to work OK. There's lots of superfluous code as I was checking along the way that it worked. The script picks up the large uploaded file after it's been moved from it's temporary location, does it's stuff, then saves it back in the same directory. $filename = $upload_name; echo' <img src="' . $filename . '" ><br/> '; // Set a maximum height and width $width = 500; $height = 500; echo' <p>Required image image width = ' . $width . ' and height = ' . $height . '</p> '; // Get new dimensions list($width_orig, $height_orig) = getimagesize($filename); echo' <p>Current image width = ' . $width_orig . ' and image height = ' . $height_orig . '</p> '; $ratio_orig = $width_orig/$height_orig; if ($width/$height > $ratio_orig) { $width = $height*$ratio_orig; } else { $height = $width/$ratio_orig; } echo' <p>New image width = ' . $width . ' and height = ' . $height . '</p> '; // Resample $image_p = imagecreatetruecolor($width, $height); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); // Save the image imagejpeg($image_p, '../uploaded_snippets/' . $name.'.jpg'); // Free up memory imagedestroy($image_p); echo' <img src="../uploaded_snippets/' . $name.'.jpg" ><br/> '; ?> Hope this helps. Cheers Paul Link to comment Share on other sites More sharing options...
leo Posted November 12, 2012 Author Share Posted November 12, 2012 thats great paul il take alook, im glad you all could help. Kind regards Link to comment Share on other sites More sharing options...
leo Posted December 5, 2012 Author Share Posted December 5, 2012 here is my image query echoing an image.... but how do i pass this image path into the getimagesize() Function to retrieve my width and height values ive tried getimagesize($img) and getimagesize('uploads/$img') can anybody help $cquery = "SELECT * FROM img_uploaded"; $test2 = mysqli_query($dbc, $cquery); while ($comm = mysqli_fetch_array($test2, MYSQLI_ASSOC)){ $img = $comm['img_name']; $imgsize = $comm['img_size']; echo "<img class=\"blogpic\" src=\"uploads/$img\" />"; Link to comment Share on other sites More sharing options...
margaux Posted December 5, 2012 Share Posted December 5, 2012 getimagesize() requires the full path to the image relative to the script invoking the function, so if getimagesize is used in an included script you need to take that into account when coding your path. getimagesize('uploads/$img') will be interpreted literally, you need to concatenate the $img variable getimagesize('uploads/' . $img) Link to comment Share on other sites More sharing options...
leo Posted December 5, 2012 Author Share Posted December 5, 2012 i figure the problem out, windows localhost doesn't have the php image library, thanks for you input anyway Link to comment Share on other sites More sharing options...
Recommended Posts