Jump to content

Calculating Image Sizes For Styling


White_Lily

Recommended Posts

Okay, so i have written a php calculation that determines what the margin left and top will be of a users avatar.

 

I have a slight problem in the way that the script can't find the image to get the size of it.

 

The Error:

 

Warning: getimagesize(http://janedealsart.co.uk/social/users/Guest/uploads/50a3c6fe2a699.jpg) [function.getimagesize]: failed to open stream: Connection refused in /home/sites/janedealsart.co.uk/public_html/social/profile.php on line 22

 

This is the code I have written for it:

 

$getbg = select("*", "users", "username = '$u'");
$bg = mysql_fetch_assoc($getbg);
$pic = $bg["background"];
$propicmargin = $bg["avatar"];
if($propicmargin){
list($width, $height) = getimagesize($propicmargin); // <-- LINE 22

/*//Calculates Height of images that have an exact width of the box.
if($width == 166){
$margin = 166-$height;
$margin = $margin/2;
if($margin < 0){ $margin = 0; }
}*/

//Calculates Width & Height of small images to find the margin left & top.
if($width < 166 && $height < 166){
$margin = 166-$height;
$margin = $margin/2;
if($margin <= 0){ $margin = 0; }
$marginw = 166-$width;
$marginw = $marginw/2;
if($marginw <= 0){ $marginw = 0; }
}

$avatar = '<img src="'.$propicmargin.'" />';

}else{
$avatar = '<img src="'.$GLOBALS["nav"].'images/avatar.png" />';
}

 

Any Ideas? Folder permissions are 777.

 

The image exists and the file path is correct.

Edited by White_Lily
Link to comment
Share on other sites

the error is " failed to open stream: Connection refused", your web host has security measures in place. The easiest way around this is to take Jessica's suggestion. There is a configuration to add your host/domain as trusted but that varies from host to host. You could use curl().

Link to comment
Share on other sites

Scott, sorry if this sounds rude, but if there were values in $width and $height I wouldn't really be posting here. Simply because if there were values in both variables then my problem would be around the actual maths part itself, this means just reconsidering the logic of events in the equation.

 

AyKay47, I have used php.ini (this is where the original connection error came from) i have used mysql_error() on the select function, and have echoed all variables possible for values, the only variables that I am concerned about is the $width, $height, and the getimagesize() php function, as these don't have any values, or have the wrong values. I have also checked the path/url provided to getimagesize() and the image existed and shows in the browser.

Link to comment
Share on other sites

Did you read the notes on list? I wonder if the fact that getimagesize returns an array with 7 indices and you're only giving list two variables and list goes right-left, would affect it.

What happens if you just print_r() the returned array from getimagesize?

Link to comment
Share on other sites

list() assigns the values from right to left, however the only time you actually need to worry about that is when you pass arrays with indices into the function. The OP is using list() correctly here. Refer to php.net Example#1.

 

White_lily I ask what debugging steps you have taken because we need to know exactly where this code is failing. If you have "echoed all variables possible for values" then you should know where the code is failing. What is the value of $propicmargin? Add a condition to determine whether getimagesize() is working correctly:

 

$size = getimagesize($propicmargin);
if(!$size)
{
 die("Could not find the size of file:<br />" . $propicmargin);
}

 

If the above code passes, $width and $height will be assigned values.

Edited by AyKay47
Link to comment
Share on other sites

Okay I have it sorted. I needed to use a file path instead of a URL, but DOCUMENT_ROOT didn't work, code below:

 

<?php
$doc = "users/".$u."/uploads/";

if(!empty($_POST['imgsubmit'])){
if (isset ($_FILES['new_image'])){
$imagename = $_FILES['new_image']['name'];
$parts = explode( '.', $imagename );
$extension = strtolower( $parts['1'] );
$new_filename = uniqid().".".$extension;
$source = $_FILES['new_image']['tmp_name'];
$target = $doc.$new_filename;
move_uploaded_file($source, $target);

$imagepath = $new_filename;
$save = $doc.$imagepath; //This is the new file you saving
$file = $doc.$imagepath; //This is the original file

list($width, $height) = getimagesize($file) ;

$modwidth = 166;

$diff = $width / $modwidth;

$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $save, 100) ;

if(!empty($width)){

$imageavi = $doc.$imagepath;

$changeavi = update("users", "avatar = '$imageavi'", "username = '$u'") or die(mysql_error());
if($changeavi){
echo "Avatar Changed!";
}
}else{

}

}
}
?>

 

I hate PHP >_> lol

Edited by White_Lily
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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