BigTime Posted February 23, 2012 Share Posted February 23, 2012 Hi there I have an image in a directory, that I need to resize on the fly but has to be a background in a div with a max width. I have to determine the correct height according to this max width. So I sucessfully I have accomplished this, but its not scaling its jsut giving me the top left corner according to my height width properties. Here is my working code to grab height and width: $img_path = $CONFIG["full_url"].$CONFIG["upload_folder"].ReadDB($News["image"]); $max_width = 110; $max_height = 400; list($width, $height) = getimagesize($img_path); $ratioh = $max_height/$height; $ratiow = $max_width/$width; $ratio = min($ratioh, $ratiow); // New dimensions $mywidth = intval($ratio*$width); $myheight = intval($ratio*$height); Now using those I need to resize the image proper to get it centered up. I dont really want to save the file I just want to display it. Is this possible? My attempts have garnered a bunch of crazy characters on screen and not an image. Here is the code I added to the end: // Resample $image_p = imagecreatetruecolor($mywidth, $myheight); $image = imagecreatefromjpeg($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $mywidth, $myheight, $width, $height); // Output imagejpeg($image_p, null, 100); Then Im using it on the page like so: <div class="theimg" style="display:block; width:<?php echo $mywidth;?>px; height:<?php echo $myheight;?>px; background-image:url(<?php echo $image_p; ?>); background-repeat: no-repeat;"/> </div> and heres what I get :/ What am I doing wrong?? The image is not displaying inside its rounded corner border and we have crazy crap on top. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/257596-resizing-scaling-image/ Share on other sites More sharing options...
BigTime Posted February 23, 2012 Author Share Posted February 23, 2012 oops I half solved my issue I see its returning: <div class="theimg" style="display:block; width:110px; height:146px; background-image:url(Resource id #12); background-repeat: no-repeat;"/> </div> What is the proper way to set Resource id #12 output to screen so Im getting the resized image? Quote Link to comment https://forums.phpfreaks.com/topic/257596-resizing-scaling-image/#findComment-1320312 Share on other sites More sharing options...
requinix Posted February 23, 2012 Share Posted February 23, 2012 You need a URL for that resized image. Something like resize.php?newsid=123&width=110&height=146 The script then looks up the image to resize and outputs a resized version. Basically, what you have now but in its own script*. The "crazy characters" are the binary image data. They're good. But you have to tell the browser that it should display as an image, rather than the HTML it assumes. Before you output anything, header("Content-Type: image/jpeg"); * It doesn't have to be its own script - you could reuse an existing script for it. The issue is that you output the image and only the image. No HTML or anything else with it. Quote Link to comment https://forums.phpfreaks.com/topic/257596-resizing-scaling-image/#findComment-1320314 Share on other sites More sharing options...
BigTime Posted February 23, 2012 Author Share Posted February 23, 2012 requinix Thank you for the help for anyone finding this is via a search my working code implemented on my subsequent independent script (resizeit.php) as suggested was: $img_path = $img; $max_width = 110; $max_height = 400; list($width, $height) = getimagesize($img_path); $ratioh = $max_height/$height; $ratiow = $max_width/$width; $ratio = min($ratioh, $ratiow); // New dimensions $mywidth = intval($ratio*$width); $myheight = intval($ratio*$height); // Resample $image_p = imagecreatetruecolor($mywidth, $myheight); $image = imagecreatefromjpeg($img_path); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $mywidth, $myheight, $width, $height); // Output header('Content-Type: image/jpeg'); imagejpeg($image_p, null, 100); echo $image_p; and in my div tag I called to (just passed the image I wanted via img=): background-image:url(<?php echo $CONFIG["full_url"].$CONFIG["upload_folder"];?>resizeit.php?img=<?php echo ReadDB($News["image"]); ?>); I used this as a workaround to implement curvycorners on images in a loop Quote Link to comment https://forums.phpfreaks.com/topic/257596-resizing-scaling-image/#findComment-1320524 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.