Hi

Many years ago... i was storing images in mysql database the time has now come to store them on my server to free up space. can any one direct me or tell me how to convert the blob images back to image files and store them within my image folder. it would be also helpful to set the size dimensions so all images are stored with equal dimensions and name each image with the title column or the row in which it was stored.

thanks Gareth

    You can easily use [man]imagecreatefromstring/man to create an image resource from the blob text, then use the typical image manipulation functions to resize as you see fit.

    You could write a function to loop through an array of mysql results to convert them and store them. For example:

    <?php
    
    mysql_connect('localhost', 'username', 'password');
    mysql_select_db('database');
    
    $query = "SELECT * FROM `images_table`";
    $result = mysql_query($query);
    
    if(!$result)
    {
        die('No images to convert to files found.');
    }
    
    $images = array();
    while($row = mysql_fetch_array($result))
    {
        $images[] = $row;
    }
    
    saveMyImages($images, '', 320, 240, 90);
    
    function saveMyImages($imgArray, $dir='./', $width=640, $height=480, $quality=80)
    {
        if($dir === false) 
        {
            $dir = './';
        }
    
    foreach($imgArray as $image)
    {
        $title = strtolower(str_replace(array(' ', "\t", "\n", "\r"), array('_'), $image['image_title']));
        $img = imagecreatefromstring($image['blob_column']);
    
        // Resize the image using GD functions (imagecopy or imagecopyresampled)        
        $o_height = imagesy($img);
        $o_width = imagesx($img);
    
        // Proportionately resize image
        if($o_height > $o_width)
        {
            $width = (($height / $o_height) * $o_width);
        }
        else
        {
            $height = (($width / $o_width) * $o_height);
        }
    
        $new = imagecreatetruecolor($width, $height);
        imagecopyresampled($new, $img, 0, 0, 0, 0, $width, $height, $o_width, $o_height);
    
        imagejpeg($new, $dir.$title.'.jpg', $quality);
    }
    }

    I make no guarantees about the code above; but it should push you in the right direction.

      Write a Reply...