0

Let me know how to fetch image from database. actually i am trying to fetch image from database using this question. I am try to fetch image from database using following code. $fid is my $row['column_name']

 $fid=$r['field_upload_photo_fid'];
 $image = theme('image_style', array(
'style_name'=> 'small', 
'path' => $file->uri, 
'alt' => 'user picture'
 ));
 print $image;

Actual file url is http://example.com/sites/default/files/styles/Style_name/public/Chrysanthemum.jpg

but now it shows http://example.com/sites/default/files/styles/small/public/styles/Style_name/public/Chrysanthemum.jpg. thats why image not fetching from database.

Also what is a style in $variable.

2 Answers 2

0

You have to replace 'Style_name' with an actual image style, then it should work. That's because the function you are using does not only fetch the url of the image, it actually tries to fetch the image for a particular style, not the original.

Per default drupal ships with the image styles 'thumbnail', 'medium' and 'large'. You get these names when going to 'admin/config/media/image-styles', open the style you want to use for your image and look at the machine name - that's the 'style_name' you need.

So in your above example use for example:

$variables = array(
 'path' => $file->uri,
 'style_name'=>'medium'
);

You can use a custom image style as well of course.


Edit:

From the comments I guess you do not actually want to retrieve the url for a styled image, but the original url of the image. Here is the code which should retrieve the file url:

$fid=$r['field_upload_photo_fid'];
$file = file_load($fid);
$url_to_your_file = file_create_url($file->uri);
7
  • after putting medium url is example.com/sites/default/files/styles/Medium/public/styles/… Mar 20, 2015 at 14:56
  • That's alright. When using the url drupal will generate the image with the measures on the 'medium' image style - per default 220x220 pixel. Do you want an image style at all or do you actually just want the original? Your code is for returning the url for a particular imagestyle
    – Zuzuesque
    Mar 20, 2015 at 14:59
  • the generated url is wrong thats why problem is create Mar 20, 2015 at 15:00
  • 1
    It is not wrong. Drupal creates an own directory for each image style, filling it with the correctly sized and cropped images. If you want the original image url just use the file_create_url() function.
    – Zuzuesque
    Mar 20, 2015 at 15:02
  • actually problem is generated url is not right thats why the image path is wrong Mar 20, 2015 at 15:03
0

The style is an image style which is described a bit in Working with images in Drupal 7 and 8. I believe that you need to use Style_name as the 'style' in your code instead of 'public':

 $fid=$r['field_upload_photo_fid'];
 $file = file_load($fid);
 $variables = array(
 'path' => $file->uri,
 'style'=>'Style_name'
 );
 print theme('image_style', $variables);

But also, what is the data in $file->uri? If you start with bad data then the result is also going to be bad.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.