Jump to content

mahalleday

Members
  • Posts

    36
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

mahalleday's Achievements

Member

Member (2/5)

0

Reputation

  1. I found the error. Would you believe it was just a typo?!? thanks for everyone's help on this. Here is the fully working code for others to make use of. <?php //get stuff $image = $_GET['image']; $max_size = $_GET['size']; $quality = $_GET['quality']; // if(file_exists($image)) { //get image info list($ow, $oh, $type) = getimagesize($image); $mime = image_type_to_mime_type($type); //output header based on image type switch($mime) { case "image/jpg": $header = 'Content-Type: image/jpeg'; break; case "image/jpeg": $header = 'Content-Type: image/jpeg'; break; case "image/png": $header = 'Content-Type: image/png'; break; case "image/gif": $header = 'Content-Type: image/gif'; break; } //header($header); //check if image is larger the $max_size if($ow > $max_size || $oh > $max_size) { //image dim(s) are larger therfore resize $ratio = $ow/$oh; //if width > height if($ow > $oh) { $nw = $max_size; $nh = $nw/$ratio; } //hegith > width else if($oh > $ow) { $nh = $max_size; $nw = $nh * $ratio; } //width and height are equal else { $nw = $max_size; $nh = $max_size; } } //the image is already an appropriate size else { $nw = $ow; $nh = $oh; }//end of image size check //decide which type of image is being sesized and convert to .jpeg switch($mime) { case "image/jpg": $new_image = imagecreatefromjpeg($image); break; case "image/jpeg": $new_image = imagecreatefromjpeg($image); break; case "image/png": $new_image = imagecreatefrompng($image); break; case "image/gif": $new_image = imagecreatefromgif($image); break; } // create blank image with new width and height $blank_image = imagecreatetruecolor($nw, $nh); //copy old image into new image with new dimensions imagecopyresampled($blank_image, $new_image, 0, 0, 0, 0, $nw, $nh ,$ow ,$oh); // Output resized image switch($mime) { case "image/jpg": imagejpeg($blank_image,NULL,$quality); break; case "image/jpeg": imagejpeg($blank_image,NULL,$quality); break; case "image/png": imagepng($blank_image); break; case "image/gif": imagegif($blank_image); break; } //free memory imagedestroy($blank_image); } else { echo "<p>File does not exist...</p>"; } ?> Thanks again. Mike.
  2. Thanks I'll try that and see what the errors say if any are present. the code above is my most current code. Works for jpgs and pngs.
  3. Here is my GD info GD Support enabled GD Version bundled (2.0.34 compatible) FreeType Support enabled FreeType Linkage with freetype FreeType Version 2.3.9 GIF Read Support enabled GIF Create Support enabled JPEG Support enabled libJPEG Version 6b PNG Support enabled libPNG Version 1.2.37 WBMP Support enabled XBM Support enabled I thought it might have something to do with my php settings but things look ok to me. I will try out the link suggested for phpThumb. In the end though I would prefer to write my own.
  4. The resize script is located in the same root directory the images folder is ie mysite.com/resizer.php and mysite.com/images. I have changed the path the be a relative one ie resizer.php?image=images/image.png. With this script I posted above both pngs and jpgs work fine the only issue is with gif images. I have added a check to see if the image I am trying to resize exists and that returns true like it should I just get the "The image “images/image.gif” cannot be displayed because it contains errors" error. Mike.
  5. Still having issues even with the switch for differnt mime types. The crazy thing is if i print out the $header and $mime variables to the screen they are correct for the type of image being resized. Here is the code again with the changes I have made.... <?php //get stuff $image = $_GET['image']; $size = $_GET['size']; $quality = $_GET['quality']; //make thumbnail method function makeThumb($image,$max_size,$quality){ //get image info list($ow, $oh, $type) = getimagesize($image); $mime = image_type_to_mime_type($type); //output header based on image type switch($mime) { case "image/jpg": $header = 'Content-Type: image/jpeg'; break; case "image/jpeg": $header = 'Content-Type: image/jpeg'; break; case "image/png": $header = 'Content-Type: image/png'; break; case "image/gif": $header = 'Content-Type: image/gif'; break; } header($header); //check if image is larger the $max_size if($ow > $max_size || $oh > $max_size) { //image dim(s) are larger therfore resize $ratio = $ow/$oh; //if width > height if($ow > $oh) { $nw = $max_size; $nh = $nw/$ratio; } //hegith > width else if($oh > $ow) { $nh = $max_size; $nw = $nh * $ratio; } //width and height are equal else { $oh = $max_size; $nh = $max_size; } } //the image is already an appropriate size else { $nw = $ow; $nh = $oh; }//end of image size check //decide which type of image is being sesized and convert to .jpeg switch($mime) { case "image/jpg": $new_image = imagecreatefromjpeg($image); break; case "image/jpeg": $new_image = imagecreatefromjpeg($image); break; case "image/png": $new_image = imagecreatefrompng($image); break; case "image/gif": $new_image = imagecreatefromgif($image); break; } // create blank image with new width and height $blank_image = imagecreatetruecolor($nw, $nh); //copy old image into new image with new dimensions imagecopyresampled($blank_image, $new_image, 0, 0, 0, 0, $nw, $nh ,$ow ,$oh); // Output resized image switch($mime) { case "image/jpg": imagejpeg($blank_image,NULL,$quality); break; case "image/jpeg": imagejpeg($blank_image,NULL,$quality); break; case "image/png": imagepng($blank_image); break; case "image/gif": imagegif($blank_image); break; } //free memory imagedestroy($blank_image); }//end //make thumb makeThumb($image,$size,$quality); ?>
  6. I'll try the header switch, I thought that might be it.
  7. I have written a php script to dynamically resize an image an covert it's file type to jpeg. <?php //include_once('ezuploader.php'); header('Content-Type: image/jpeg'); //get stuff $image = $_GET['image']; $size = $_GET['size']; $quality = $_GET['quality']; //make thumbnail method function makeThumb($image,$max_size,$quality){ if(file_exists($image)) { //get image extension $parts = explode('.',$image); $ext = $parts[1]; //get the width and height of origonal image list($ow, $oh) = getimagesize($image); //check if image is larger the $max_size if($ow > $max_size || $oh > $max_size) { //image dim(s) are larger therfore resize $ratio = $ow/$oh; //if width > height if($ow > $oh) { $nw = $max_size; $nh = $nw/$ratio; } //hegith > width else if($oh > $ow) { $nh = $max_size; $nw = $nh * $ratio; } //width and height are equal else { $oh = $max_size; $nh = $max_size; } } //the image is already an appropriate size else { $nw = $ow; $nh = $oh; }//end of image size check //decide which type of image is being sesized and convert to .jpeg switch($ext) { case "jpg": $new_image = imagecreatefromjpeg($image); break; case "jpeg": $new_image = imagecreatefromjpeg($image); break; case "png": $new_image = imagecreatefrompng($image); break; case "gif": $new_image = imagecreatefromgif($image); break; } // create blank image with new width and height $blank_image = imagecreatetruecolor($nw, $nh); //copy old image into new image with new dimensions imagecopyresampled($blank_image, $new_image, 0, 0, 0, 0, $nw, $nh ,$ow ,$oh); // Output imagejpeg($blank_image,NULL,$quality); //free memory imagedestroy($blank_image); } else { echo 'no files exists dumby...'; } }//end //make thumb makeThumb($image,$size,$quality); ?> This is how I intend to use it... <p><img src="resizer.php?image=image_to_be_resized.png&quality=75&size=150"/></p> However I get this error when I run it.... The image “http://mysite.com/resizer.php?image=localhost/wb/modules/adbaker/images/image_to_be_resized.png&quality=75&size=150” cannot be displayed because it contains errors. I worked when I was just passing jpeg files through and I see no reason why the conversion shouldn't work. any help as always is much appreciated. Mike.
  8. Here is code that runs without errors: function makeAd($id=null){ //if ad id is given then only get that ad if($id != NULL) { $where = "WHERE ad_id = $id"; } else {$where = "";} $sql = "SELECT * FROM ads".$where; $query = mysql_query($sql) OR die($mysql_error()); //use while loop to make ad while($ret_val = mysql_fetch_array($query, MYSQL_ASSOC)): //get ads category, subcategory and images etc from other tables $cat_query = mysql_query("SELECT category_name FROM categories WHERE category_id = ".$ret_val['category_id']); $value = mysql_fetch_array($cat_query,MYSQL_ASSOC); $category = $value['category_name']; $sub_query = mysql_query("SELECT subcategory_name FROM subcategories WHERE subcategory_id = ".$ret_val['subcategory_id']); $value = mysql_fetch_array($sub_query,MYSQL_ASSOC); $subcategory = $value['subcategory_name']; $cat_query = mysql_query("SELECT image_name FROM images WHERE ad_id = ".$ret_val['ad_id']); while($value = mysql_fetch_array($cat_query,MYSQL_ASSOC)): //loop to get all images for ad $image = $value['image_name']; endwhile; //make ads array $ad = array( 'tilte'=>$ret_val['ad_title'], 'price'=>$ret_val['ad_price'], 'desc'=>$ret_val['ad_long'], 'category'=>$category, 'subcategory'=>$subcategory, 'images'=>$image ); endwhile; //return completes ad(s) return $ad; } Here is the result from print_r Array ( [tilte] => Mike's Mundane Blog [price] => 99.00 [desc] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin consequat, mauris sit amet venenatis aliquet, purus elit elementum nisl, non semper nisi augue vel justo. Vestibulum vel augue non tortor commodo porttitor. Mauris lorem est, porttitor vehicula rutrum vel, egestas at purus. Morbi sodales mauris eu mauris pulvinar dignissim. Nullam sodales porta ligula, ac aliquet quam dapibus ac. Cras vel erat eu magna scelerisque tristique. Curabitur ac mi eros, eu scelerisque ipsum. Donec non ipsum et nulla egestas sodales in quis augue. Quisque tempor dignissim nisi eu ultrices. Sed nisl erat, ultricies ac posuere id, interdum vel nunc. Duis a lorem est, sagittis sollicitudin ligula. Nunc sed urna sit amet lorem consequat porta non placerat metus. [category] => Electronics [subcategory] => Samsung [images] => sun.jpg ) There are three ads total each ads has four images. Here is the result when Itry and loop through the returned values <html> <body> <ul><li>M</li><ul><li>Price: M</li><li>Category: M</li><li>Subcategory: M</li><li>Description: M</li><li>Image: M</li></ul></ul><ul><li>9</li><ul><li>Price: 9</li><li>Category: 9</li><li>Subcategory: 9</li><li>Description: 9</li><li>Image: 9</li></ul></ul><ul><li>L</li><ul><li>Price: L</li><li>Category: L</li><li>Subcategory: L</li><li>Description: L</li><li>Image: L</li></ul></ul><ul><li>E</li><ul><li>Price: E</li><li>Category: E</li><li>Subcategory: E</li><li>Description: E</li><li>Image: E</li></ul></ul><ul><li>S</li><ul><li>Price: S</li><li>Category: S</li><li>Subcategory: S</li><li>Description: S</li><li>Image: S</li></ul></ul><ul><li>s</li><ul><li>Price: s</li><li>Category: s</li><li>Subcategory: s</li><li>Description: s</li><li>Image: s</li></ul></ul><ul><li>M</li><ul><li>Price: 9</li><li>Category: E</li><li>Subcategory: S</li><li>Description: L</li><br /> <b>Warning</b>: Invalid argument supplied for foreach() in <b>C:\wamp\www\oophp\test.php</b> on line <b>42</b><br /> </ul></ul><ul><li>i</li><ul><li>Price: 9</li><li>Category: l</li><li>Subcategory: a</li><li>Description: o</li><br /> <b>Warning</b>: Invalid argument supplied for foreach() in <b>C:\wamp\www\oophp\test.php</b> on line <b>42</b><br /> </ul></ul><ul><li>k</li><ul><li>Price: .</li><li>Category: e</li><li>Subcategory: m</li><li>Description: r</li><br /> <b>Warning</b>: Invalid argument supplied for foreach() in <b>C:\wamp\www\oophp\test.php</b> on line <b>42</b><br /> </ul></ul> <body> </html> Here is the loop I used to get this: foreach($ads as $ad): echo '<ul>'; echo '<li>'.$ad['title'].'</li>'; echo '<ul><li>Price: '.$ad['price'].'</li>'; echo '<li>Category: '.$ad['category'].'</li>'; echo '<li>Subcategory: '.$ad['subcategory'].'</li>'; echo '<li>Description: '.$ad['description'].'</li>'; foreach($ad['images'] as $image): echo '<li>Image: '.$ad['image_name'].'</li>'; endforeach; echo '</ul></ul>'; endforeach;
  9. I'll try that and see what happens. I also noticed a ton of syntax and other errors in the code I wrote in. thats what I get for not just copying the code over. I'll post "working code" shortly hopefully with more correct code someone can help me out. Sorry about that. Mike
  10. I have written a method that gathers data from several tables in my database and builds a ad out of the information then returns multidimensional array for each ad like this: $ad[0] $ad['title'][0] $ad['price'][0] $ad['description'][0]...etc The method i have looks something like this: function makeAd($id=null){ //if ad id is given then only get that ad if($id != NULL) { $where = "WHERE ad_id = $id"; } else {$where = "";} $sql = "SELECT * FROM ads $where"; $query = mysql_query($sql); //use while loop to make ad while($ret_val = mysql_fetch_array($query, MYSQL_ASSOC)): //get ads category, subcategory and images etc from other tables $cat_query = mysql_query("SELECT cat_name FROM categories WHERE $category_id = ".$ret_val['category_id']); $value = mysql_fetch_array($cat_query,MYSQL_ASSOC); $category = $value['cat_name']; $sub_query = mysql_query("SELECT sub_name FROM categories WHERE $subcategory_id = ".$ret_val['subcategory_id']); $value = mysql_fetch_array($sub_query,MYSQL_ASSOC); $category = $value['sub_name']; $cat_query = mysql_query("SELECT image_name FROM categories WHERE $ad_id = ".$ret_val['ad_id']); while($value = mysql_fetch_array($cat_query,MYSQL_ASSOC)): //loop to get all images for ad $image = $value['image_name']; endwhile; //make ads array $ad = array( 'tilte'=>$ret_val['title'], 'price'=>$ret_val['price]', 'desc'=>$ret_val['description'], 'category'=>$category, 'subcategory'=>$subcategory, 'images'=>$image ); endwhile; //return completes ad(s) return $ad; } Then I loop through the returned array like this... include(myClass.php); $class = new myClass(); $ads = $class->makeAd(); foreach($ads as $ad): echo '<ul>' echo '<li>'.$ad['title'].'</li>'; echo '<ul><li>Price: '.$ad['price'].'</li>'; echo '<li>Category: '.$ad['category'].'</li>'; echo '<li>Subcategory: '.$ad['subcategory'].'</li>'; echo '<li>Description: '.$ad['description'].'</li>'; foreach($ads['images'] as $image): echo '<li>Image: '.$image['image_name'].'</li>'; endforeach; echo '</ul></ul>'; endforeach; I get no errors but I do not get correct results either just random letters and numbers for each item. I am sure it has to do with how I have handled looping through the multidimensional array but can;t sort it out. Any help/coding advice would be greatly appreciated. Please also note that the html in the loop is only there for testing/development and is just thrown together. Thanks. Mike.
  11. Just bumping this back up. Hopefully someone can give me a hand here.
  12. Ok finally back on my home machine, here is the actual class I have made and the code used to access the file and output the info from the database in the browser. class_myclass.php <?php class myClass{ var $id; //set data method function setData($data) {$this->id=$data;} //function to get number os total posts function postCount() { $sql = "SELECT post_id FROM blog_posts"; $query = mysql_query($sql); $no_posts = mysql_num_rows($query); return $no_posts; } //function to retreve dat from databse table function getPost() { if(isset($this->id)) { $sql = "SELECT * FROM blog_posts WHERE post_id=".$this->id; } else { $sql = "SELECT * FROM blog_posts"; } $query = mysql_query($sql) or die($data = "<p>SQL: $csql</p><p>".mysql_error()."</p>"); while($posts = mysql_fetch_array($query,MYSQL_ASSOC)) { $cats = self::getCategory($posts['category_id']); $sub_cats = self::getSubCategory($posts['subcategory_id']); $images = self::getImages($posts['post_id']); $data = array( 'post_id'=>$posts['post_id'], 'post_title'=>$posts['post_title'], 'post_short'=>$posts['post_short'], 'post_long'=>$posts['post_long'], 'category_name'=>$posts['category_id'], 'subcategory_name'=>$posts['subcategory_id'], 'image'=>$images['image_name'], 'post_start'=>$posts['post_start'], 'post_end'=>$posts['post_end'] ); } return $data; } //method to get category for post function getCategory($id = NULL,$list = 0) { $data = array(); if($list == 0 AND $id = NULL) { $sql = "SELECT * FROM blog_categories WHERE category_id = $id"; } else { $sql = "SELECT * FROM blog_categories"; } $query = mysql_query($sql) OR die($data = "<p>SQL: $sql</p><p>".mysql_error()."</p>"); while($ret_val = mysql_fetch_array($query,MYSQL_ASSOC)){ $category[] = $ret_val; } return $category; } //method to get subcategory for post function getSubCategory($id = NULL,$list = 0) { $data = array(); if($list == 0 AND $id = NULL) { $sql = "SELECT * FROM blog_subcategories WHERE category_id = $id"; } else { $sql = "SELECT * FROM blog_subcategories"; } $query = mysql_query($sql) OR die($data = "<p>SQL: $sql</p><p>".mysql_error()."</p>"); while($ret_val = mysql_fetch_array($query,MYSQL_ASSOC)){ $subcategory[] = $ret_val; } return $subcategory; } //method to get images from database function getImages($id) { $sql = "SELECT * FROM blog_images WHERE post_id = $id"; $query = mysql_query($sql) OR die($data = "<p>SQL: $sql</p><p>".mysql_error()."</p>"); while($ret_val = mysql_fetch_array($query,MYSQL_ASSOC)) { $images[] = $ret_val; } return $images; } }//end myClass index.php <?php include('class_myClass.php'); $myClass1 = new myClass(); $no_posts = $myClass1->postCount(); for($i=0;$i<=$no_posts-1;$i++) { $myClass1->setData($i+1); $posts[$i]=$myClass1->getPost(); } $path = "images/"; foreach($posts as $post) { $images=$myClass1->getImages($post['post_id']); $cats=$myClass1->getCategory($post['category_name']); $subs=$myClass1->getSubCategory($post['subcategory_name']); ?> <table> <tr> <td> <p> <?php echo $post['post_title'];?> Posted on: <?php echo $post['post_start'];?> Category: <?php echo $cats['category_name'];?> <span style="float: right"> <?php foreach($images as $img) {echo '<img src="'.$path.$img['image_name'].'" alt="alt text" width="50" height="50"/>';}?> </span> </p> <p><?php echo $post['post_short'];?></p> <a href="#">Click to read more...</a> </td> </tr> </table> <?php $i++;}//end while ?> I have attached a screen shot of what happens in the browser. No formatting or CSS implemented yet obviously the final product will look a lot better. Ideally I would like to have just one method called in index.php (getPost()) and have the other methods used within the getPost() method. Sorry if there is any confusion with me using this in terms of a blog but I plan to port the code over for the classifies project the blog tuff is being used cause it is simpler then what I plan on doing with the classifieds. [attachment deleted by admin]
  13. I am pretty new to object orientated php so I'm not too sure how the method works exactly. It looks like it is depending an other database class to retrieve to info from the database. Perhaps I am missing some thing here can you explain?
  14. I am building a classified ads module for a CMS I use and want to use a class to 'build' each ad before it is displayed on the browser. What I have is something like this. <?php class classifedAds { var $id; //set method function setData($data) { $this->id=$data; } //ad method function getAd() if(isset($this-id)) { $sql = "SELECT * FROM ads WHERE ad_id =".$this->id; } else { $sql = "SELECT * FROM ads"; } $query = mysql_query($sql); while($data = mysqk_fetch_array($query, MYSQL_ASSOC)) { //get ad images $images = self::getImages($data['ad_id']); //get ad category name $category = self::getCategory($data[cat_id]); $ad = array( 'title'=> $data['title'], 'desc'=> $data['desc'], 'price'=> $data['price'], 'image'=> $images['image'], 'category'->$category['category_name'] ); return $ad; }//end getAd //get images method function getImages($ad_id) { $sql = "SELECT image FROM images WHERE ad_id=$ad_id"; $query = mysqL_query; $images = mysql_fetch_arry($query, MYSQL_ASSOC); return $images; //get categories method function getImages($ad_id) { $sql = "SELECT category FROM categories WHERE cat_id=$cat_id"; $query = mysqL_query; $category = mysql_fetch_arry($query, MYSQL_ASSOC); return $category; } }//end class ?> This works ok if i set an ad id first like this.... <?php $class = new classifedAds; $class -> setData($some_ad_id_number); $ad = $class->getAd(); ?> But because I need to display a list of ads same on the main page I would like to just do this... <?php $class = new classifedAds; $ad = $class->getAd(); ?> Which does not work at all. I'll post a screen shot later when I get home and run it. Hopefully someone here can point me in the right direction. The other Issue I seem to have is being able to loop through ads images. So assuming that the getAd() method works fine I try this to get the images to display. <?php foreach($ad['image'] as $img) { echo '<img scr="/path_to_images/'.$img.'"/>'; } ?> This outputs something like this... <img scr="/path_ti_images/(some random letter)"/> for each of the images in the $ad['image'] array. Any help on either of these issues is greatly appreciated.
  15. Does anyone have or know of an easy to use php form generation class that can accept data pulled from mysql database tables? I have started to build my own but I because my oo php skills are somewhat limited I know there has to be some one out there who has already done this are far better than anything I have written. Why re-invent the wheel right. Here is the method I wrote to pull an associative array from a database and create a select list. //create select box from database function formSelectBoxDB($inputs, $sql, $val, $key) { $query = mysql_query($sql); $select = '<select'; $select .= ' name="'.$inputs['name']; $select .= '" id="'.$inputs['id']; $select .= '" class="'.$inputs['class'].'">'; $select .= '<option value="">Select '.$inputs['id'].'</option>'; while($rows = mysql_fetch_array($query, MYSQL_ASSOC)) { $select .= '<option'; $select .= ' value="'.$rows[$key].'">'; $select .= $rows[$val]; $select .= '</option>'; $i++; } $select .= '</select>'; return $select; }
×
×
  • 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.