Show only certain images

images php coding issues or problems here.

Moderators: macek, egami, gesf

public
New php-forum User
New php-forum User
Posts: 4
Joined: Mon May 06, 2013 3:16 am

Show only certain images

Postby public » Mon May 06, 2013 3:18 am

I have a table with images in BLOBs. I want to show one of those images where the height isn't bigger than 100px and the width not bigger than 120px. If such an image isn't found, it will display a standard image.

I'm getting this error:

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/server15/public_html/cards/view.php on line 50


Code: Select all

<?php
class MySQL
{
/*
* Set up private variables for connection
*/
// private $host   = 'localhost';
// private $user   = 'admin';
// private $pass   = 'root';
// private $dbname = 'database';


private $host   = 'localhost';
private $user   = 'admin';
private $pass   = 'root';
private $dbname = 'database';


/*
* Error messages variables
*/
private $error_open = '<div style="color: red;">';
private $error_close = '</div>';


/*
* Create a construct method to handle the connection
*/
function __construct()
{
$host   = $this->host;
$user   = $this->user;
$pass   = $this->pass;
$dbname = $this->dbname;


$this->sql_link = mysql_connect($host, $user, $pass) or die('error 1!');
mysql_select_db($dbname, $this->sql_link) or die('error!');
}


/*
* Create query method to select things from the mysql database
*/
function query($sql)
{
return mysql_query($sql);
}


/*
* Create fetch_object method to fetch the previously ran query
*/
function fetch_object($query)
{
return mysql_fetch_object($query);
}


function fetch_array($query)
{
return mysql_fetch_array($query);
}


/*
* Create num_rows method to fetch the previously ran query rows in an int
*/
function num_rows($query)
{
return mysql_num_rows($query);
}


/*
* Escape chars from SQL Injection
*/
function escape($string)
{
return mysql_real_escape_string($string);
}


}
$db = new MySQL();
$loadCards = $db->query("SELECT * FROM `cardId` WHERE `status` = '1' ORDER BY `id` ASC LIMIT 10 ");
while ($row = $db->fetch_object($loadCards))
{
$loadImages = $db->query("SELECT * FROM `images` WHERE `cid` = '$row->id'");
while ($rowIMG = $db->fetch_object($loadImages))
{
$img = imagecreatefromstring($rowIMG->img);
$height = $imagesy($img);
$width = imagesx($img);
if (($height > 100) OR ($width > 120))
{
echo '<img src="noimage.png" />';
} else {
echo '<img src="';
echo image($img);
echo ' ">';
}
}
}
?>


Does anyone know how to fix this?

User avatar
sandeep605085
New php-forum User
New php-forum User
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions
Contact:

Re: Show only certain images

Postby sandeep605085 » Mon May 06, 2013 4:55 am

Hi,

I guess there is a problem in executing either of the below sql query.

"SELECT * FROM `cardId` WHERE `status` = '1' ORDER BY `id` ASC LIMIT 10 ";
"SELECT * FROM `images` WHERE `cid` = '$row->id'";

please echo the query and check it by running it directly from mysql command line or mysql query browser to check if query is getting executed.

Thanks,
Sandeep Agrawal
Mindfire Solutions

public
New php-forum User
New php-forum User
Posts: 4
Joined: Mon May 06, 2013 3:16 am

Re: Show only certain images

Postby public » Tue May 07, 2013 1:42 am

I've 'cleaned' the file and it's now like this (the row img from the tabel images stores the blob image file):

Code: Select all

<?php

$pdo = new PDO('mysql:host=localhost;dbname=database', 'admin', 'root');

$query = $pdo->query("SELECT * FROM `cardId` WHERE `status` = '1' ORDER BY `id` ASC LIMIT 10 ");
$results = $query->fetchAll();

$query2 = $pdo->query("SELECT * FROM `images` WHERE `cid` = '$results->id'");
$results2 = $query2->fetchAll();

$img = imagecreatefromstring($results2->img);
$height = $imagesy($img);
$width = imagesx($img);
if (($height > 100) OR ($width > 120))
{
   echo '<img src="noimage.png" />';
} else {
   echo '<img src="';
   echo image($img);
   echo ' ">';
}
?>


The following errors appear on my screen:

Warning: imagecreatefromstring() [function.imagecreatefromstring]: Empty string or invalid image in /home/server15/public_html/cards/view.php on line 11

Fatal error: Function name must be a string in /home/server15/public_html/cards/view.php on line 12

User avatar
sandeep605085
New php-forum User
New php-forum User
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions
Contact:

Re: Show only certain images

Postby sandeep605085 » Tue May 07, 2013 6:33 am

Please check at below line
$img = imagecreatefromstring($results2->img);

try echo $results2->img; and check that if it is giving a blank value or any thing other than string as
"imagecreatefromstring" accepts a string containing the image data.

then

$height = $imagesy($img);

is $imagesy is a function? i guess $ should not be there and it should be
$height = imagesy($img);

Thanks,
Sandeep Agrawal
Mindfire Solutions.

public
New php-forum User
New php-forum User
Posts: 4
Joined: Mon May 06, 2013 3:16 am

Re: Show only certain images

Postby public » Tue May 07, 2013 6:55 am

This is my code now:

Code: Select all

<?php

$pdo = new PDO('mysql:host=localhost;dbname=database', 'admin', 'root');

$query = $pdo->query("SELECT * FROM `cardId` WHERE `status` = '1' ORDER BY `id` ASC LIMIT 10 ");
$results = $query->fetchAll();

$query2 = $pdo->query("SELECT img FROM `images` WHERE `cid` = '$results->id'");
$results2 = $query2->fetchAll();

$img = imagecreatefromstring($results2);
$height = imagesy($img);
$width = imagesx($img);
if (($height > 100) OR ($width > 120))
{
    echo '<img src="noimage.png" />';
} else {
    echo '<img src="';
    echo image($img);
    echo ' ">';
}
?>


Warning: imagecreatefromstring() [function.imagecreatefromstring]: Empty string or invalid image in /home/server15/public_html/cards/view.php.php on line 11


Warning: imagesy(): supplied argument is not a valid Image resource in /home/server15/public_html/cards/view.php.php on line 12


Warning: imagesx(): supplied argument is not a valid Image resource in /home/server15/public_html/cards/view.php.php on line 13


If I use echo $results2;, I get Array (and the other three errors). If I use echo $results2->img;, it shows only the three errors.

User avatar
sandeep605085
New php-forum User
New php-forum User
Posts: 44
Joined: Tue Feb 26, 2013 6:00 am
Location: Mindfire Solutions
Contact:

Re: Show only certain images

Postby sandeep605085 » Tue May 07, 2013 10:18 am

the actual error is at below line that value passed to imagecreatefromstring function is not a valid string.
$img = imagecreatefromstring($results2);

$result2 will return an array and as i told you that imagecreatefromstring accepts string but now you are passing array to it.
$results2 = $query2->fetchAll();

your previous code was correct which was
$img = imagecreatefromstring($results2->img);

but you know better than me that what is the value of $results2->img. if exists or not, if any field is present in database table or not.

solution for other two error depends on $img value as you are passing $img to below two function and $img value comes from imagecreatefromstring($results2->img);

$height = imagesy($img);
$width = imagesx($img);

Thanks,
Sandeep Agrawal
Mindfire Solutions

public
New php-forum User
New php-forum User
Posts: 4
Joined: Mon May 06, 2013 3:16 am

Re: Show only certain images

Postby public » Wed May 08, 2013 12:44 am

Thanks for your help, I got it working by using another page to show the image and then checking that page to see if the image has the correct size.

xiaomingzc
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Jul 01, 2013 11:22 pm

Re: Show only certain images

Postby xiaomingzc » Mon Jul 01, 2013 11:30 pm

Passing away is not considerably of Runescape Gold the charge. do not be scared to die. it is frustrating, as well as you can accumulate some really light fix expenses. But especially like a lower-level gamer, do not be Cheap WOW Gold scared to die. Some that belong towards really best minutes within fitting can be discovered after you say, "I cannot think I resided buy diablo 3 Gold through that," as well as you cannot reach people minutes with out jeopardizing loss of life.

westwade
New php-forum User
New php-forum User
Posts: 4
Joined: Wed Jul 03, 2013 9:31 pm
Contact:

Re: Show only certain images

Postby westwade » Wed Jul 03, 2013 10:08 pm

As a result, the body can not support itself through the process of becoming pregnant; resulting in irregular ovulation times, fewer viable eggs, and even problems with the male's sperm. To combat this, couples may want to consider supplementing with vitamins good for fertility, and increase their overall chances of a pregnancy.
wow gold cheap
runescape guide


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 2 guests