Recent Topics

Image Dimensions automatically added to tags

Started by on May 25, 2004 – Contents updated: May 25, 2004

May 25, 2004 09:07    

I prefer to have the page rendered with space set aside for images before they load, so I modified /admin/b2upload.php with the following (about line 145)

if ( ereg('image/',$img1_type)) {
	$img_dimensions = getimagesize("$fileupload_url/$img1_name");
	$piece_of_code = "<img src="$fileupload_url/$img1_name" width="$img_dimensions[0]" height="$img_dimensions[1]" border="0" alt="$imgdesc" />"; 
} else {
	$piece_of_code = "<a href="$fileupload_url/$img1_name" title="$imgdesc" />$imgdesc</a>"; 

May 25, 2004 10:01

Great. That's exactly what I also thought about yesterday and put it onto my todo for the media library.. until that gets done I'll put your code onto CVS.

May 25, 2004 20:14

I just added this to the appropriate file and got the following error:

Warning: getimagesize( failed to open stream: HTTP request failed! in /home/wonderw/public_html/blog/admin/b2upload.php on line 145

It worked sort of. The image uploaded but the text provided to insert into the post was "...... width="" height="" ......." It also took a heck of a long time to decide on the error and finish it's task, if that's a clue. Any idea where I should check on what for why?

May 26, 2004 02:30

I'll buy that - and talk to my host about it to see what they can do. In the mean time I decided to comment out the "getimagesize" line and remove the content it should provide from the "$piece_of_code" line. Cute variable name eh? That way it is providing me with spaces for the width and height parameters so I just type them in after placing the tag in the new post. It's not the best, but it works for me so I'll run with it until "getimagesize" behaves.

I also modified admin/b2upload to provide a class for the image. For the record I use the custom skin and I do not allow to skin switching. Other skins may or may not have these classes. In admin/b2upload.php at line 50:

<p><?php echo T_('Description') ?>:<br />
<input type="text" name="imgdesc" size="30" class="uploadform" /></p>

<p><?php echo T_('Select Class') ?>:<br />
<input type="radio" name="selectclass" class="uploadform" value="none">none&nbsp;
<input type="radio" name="selectclass" class="uploadform" value="top">top&nbsp;
<input type="radio" name="selectclass" class="uploadform" value="middle">middle&nbsp;
<input type="radio" name="selectclass" class="uploadform" value="left" checked>leftmargin&nbsp;
<input type="radio" name="selectclass" class="uploadform" value="right">rightmargin&nbsp;

The "Description" part is original - I added the "Select Class" radio buttons. Next is an addition immediately above the "getimagesize" piece that I had to comment out, and a change to the "$piece_of_code" deal.

if ($HTTP_POST_VARS['selectclass'] == "none") {
 $imgclass = "";
if ($HTTP_POST_VARS['selectclass'] == "top") {
 $imgclass = " class=&quot;top&quot;";
if ($HTTP_POST_VARS['selectclass'] == "middle") {
 $imgclass = " class=&quot;middle&quot;";
if ($HTTP_POST_VARS['selectclass'] == "left") {
 $imgclass = " class=&quot;leftmargin&quot;";
if ($HTTP_POST_VARS['selectclass'] == "right") {
 $imgclass = " class=&quot;rightmargin&quot;";

if ( ereg('image/',$img1_type)) {
   // $img_dimensions = getimagesize("$fileupload_url/$img1_name");
   // $piece_of_code = "&lt;img src=&quot;$fileupload_url/$img1_name&quot; width=&quot;$img_dimensions[0]&quot; height=&quot;$img_dimensions[1]&quot; border=&quot;0&quot; $imgclass alt=&quot;$imgdesc&quot; /&gt;";
   $piece_of_code = "&lt;img src=&quot;$fileupload_url/$img1_name&quot; width=&quot;&quot; height=&quot;&quot; border=&quot;0&quot;$imgclass alt=&quot;$imgdesc&quot; /&gt;";
} else {
   $piece_of_code = "&lt;a href=&quot;$fileupload_url/$img1_name&quot; title=&quot;$imgdesc&quot; /&gt;$imgdesc&lt;/a&gt;";

I then tweaked my CSS file a bit to push the last line beneath the image with a "clear: both;" added to bSmallHead (which is what is what I use for my little text under the posts). I then uploaded three images with only a little text associated with each and didn't like how it looked in Mozilla. Adding "bPost p { clear: both; }" to my CSS did the trick - the text typed in after each image stays with the image, and the bottom line of stuff goes beneath the last image.

Life is Good!

May 27, 2004 09:45

getimagesize is temperamental for me too it turns out.

It works fine on my test set up, but on my webhost it ALWAYS returns width="100" height="30"! >:( (only for jpg's - it works ok for gif's)

If anyone has a reason for this please let me know

Jun 23, 2004 21:26

I do a little more mod, now it is exactly wut i want, thx for u guys otherwise i have no idea wut place should i go to since i know nothing about php. :oops:
I just add a <a href> tag before that <img> tag if the size is bigger than expected and make a it thumb, then ppl can click on the thumb to view the whole pic. :D

if ( ereg('image/',$img1_type)) {
   $img_dimensions = getimagesize("$fileupload_url/$img1_name");
   if ($img_dimensions[0]>400) {
   	   $img_width = 400;
   	   $img_height = round($img_dimensions[1]*400/$img_dimensions[0]);
   	   $piece_of_code = "&lt;a href=&quot;$fileupload_url/$img1_name&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;$fileupload_url/$img1_name&quot; width=&quot;$img_width&quot; height=&quot;$img_height&quot; alt=&quot;$imgdesc&quot; /&gt;&lt;br /&gt;Click to view original size&lt;br /&gt;&lt;/a&gt;";
   else {
   	   $piece_of_code = "&lt;img src=&quot;$fileupload_url/$img1_name&quot; border=&quot;0&quot; alt=&quot;$imgdesc&quot; /&gt;"; 
else {
   $piece_of_code = "&lt;a href=&quot;$fileupload_url/$img1_name&quot; title=&quot;$imgdesc&quot; /&gt;$imgdesc&lt;/a&gt;";

Jul 11, 2004 21:12

Hi, I used the above code to detect and control image size. it worked well in first week. when uploaded image is too wide, a width=, and height= tag will be inserted. however, it seems not working any more. I checked the upload.php file, the code is still there. don't why. any ideas? thanks.

Jul 16, 2004 23:08

Thank you! It works wonderfully!

Form is loading...

Photo gallery software – This forum is powered by b2evolution CMS, a complete engine for your website.