Jump to content

resizing / scaling image


BigTime
 Share

Recommended Posts

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 :/

x5pp5j.jpg

 

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

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.