Jump to content

image url


Frank Vèssia
 Share

Recommended Posts

I'm noticing a strange behavior of the image->url property.

I call a php file from jquery that display an image and a message so i have to put my php file outside the /site/ dir, but when i call  $image->url it returns /my-external-dir/site/assets/file/etc...

<?
require_once('../index.php');
$messages=$wire->pages->find("template=wallmessage,limit=20,sort=-created");
foreach ($messages as $message){
$username = $message->parent->parent->name;
$profilephoto = $message->parent->parent->profilephoto->eq(0)->size(60,60);
echo "<p><img src='http://{$_SERVER['HTTP_HOST']}{$profilephoto->url}' /><div><a href='/profilo/{$username}'>{$username}</a></div><div>{$message->wallmessage}</div></p>";
}
?>
Link to comment
Share on other sites

Don't know why it is so. But Ryan certainly could tell you what's up with this.

You could alternatively put the file in the root, this would work. Or if you got it in a folder in the root you could filter out the additional path segment. Simple as that:

$path = str_replace('/myfolder','',$img->url);
Link to comment
Share on other sites

Soma, can you try this test:

1. Create a subdirectory off of your PW installation directory, i.e. /test/

2. Put this file in there:

/test/test.php

<?php
include("../index.php"); 
echo $wire->pages->get("/some/page/")->url; 

If your site is installed at root, it should say:

/some/page/

If your site is installed in a subdir, it should say:

/subdir/some/page/

Are you getting something different? If so, what's the server environment? If you have phpinfo output that you want to PM me a URL to, that's all I would need to see.

Also, double check that your /index.php is the latest version from yesterday (which is where this change was made). Very often when people (at least me) update their PW install, they just replace the /wire/ dir and not the /index.php, but this is one case where you'd need to replace the /index.php in order for it to work.

Thanks,

Ryan

Link to comment
Share on other sites

Ok I got it on local server.  Normal install, latest version (with latest inde.php)

I create a strap.php in myfolder like: http://pw2.ch/myfolder/strap.php

Code in strap.php:

<?php
include('../index.php');
$p = $wire->pages->get("/about/")->children->first();

foreach($p->images as $img){
	echo "<img src='{$img->url}'/>";
}
?>

Output code is:

<img src='/myfolder/site/assets/files/1002/p1000832.jpg'/><img src='/myfolder/site/assets/files/1002/p1000840.jpg'/><img src='/myfolder/site/assets/files/1002/p1000835.jpg'/><img src='/myfolder/site/assets/files/1002/p1000176.jpg'/><img src='/myfolder/site/assets/files/1002/p1000183.jpg'/><img src='/myfolder/site/assets/files/1002/p1000184.jpg'/><img src='/myfolder/site/assets/files/1002/p1000185.jpg'/><img src='/myfolder/site/assets/files/1002/p1000186.jpg'/><img 

I sent you the phpinfo in a PM.

Thanks

Link to comment
Share on other sites

Thanks for sending that. Can you try this in your strap.php and tell me what the output is?

<?php
include("../index.php"); 
echo "<pre>";
echo $_SERVER['SCRIPT_FILENAME'] . "\n";
echo __FILE__ . "\n";

Here is the output on mine for instance (two of the same lines):

/Applications/MAMP/htdocs/skyuser/test/index.php
/Applications/MAMP/htdocs/skyuser/test/index.php

I'm guessing yours are different and we may have to use realpath(). So if they are different, try this and let me know if that does it?

<?php
include("../index.php"); 
echo "<pre>";
echo realpath($_SERVER['SCRIPT_FILENAME']) . "\n";
echo realpath(__FILE__) . "\n";

Thanks for your help testing this.

Link to comment
Share on other sites

yes my output is:

/Applications/XAMPP/htdocs/pw2.ch/myfolder/strap.php
/Applications/XAMPP/xamppfiles/htdocs/pw2.ch/myfolder/strap.php

realpath is:

/Applications/XAMPP/xamppfiles/htdocs/pw2.ch/myfolder/strap.php
/Applications/XAMPP/xamppfiles/htdocs/pw2.ch/myfolder/strap.php

already seen (i'm fast ;))

Link to comment
Share on other sites

  • 4 months later...

Hi all! I didn't know wether to do a new post or not, because it is closely related to the above.

I've been using ProcessWire 2.2 boostrapped into Codeigniter quite succesfully, until this. I am using PW as the backend and CI as the router and front end. So PW is inside the /pw/ directory in the root. In this case I am calling some PW code directly inside a CI view. I am requesting some images from an image field and the url returned is incomplete. The rest of the fields return ok.

When using image_field->url, I get "/site/assets/files/1110/" without the image name or the extension. Then I prefixed the real base url to get the correct beginning (http://www.sitename.com). After that, I tried to call the image name and ext manually with $banner->image->name and$banner->image->ext but they return nothing. Exploring the $banner variable with var_dump i found the name under "basename" but not the extension. Also, maybe worth noting: I see a lot of "protected" strings inside the var_dump (don't know if that's normal).

I know that there's some complexity in what I'm doing but I think there's something weird o I'm doing something wrong. Or maybe I am messing with something, right?

This is the full call. Note that $pw_url adds the parent's path to where PW is nested (http://www.sitename.com).

$banners_destacados = wire('pages')->get('/home/destacados/')->children('limit=3');
foreach ($banners_destacados as $banner) {
 echo  "<div class='entrada'>".
	"<h3><a href='{$banner->pastilla_link}'>{$banner->title}</a></h3>".
	"<a class='img' href='{$banner->url}'><img src='{$banner->pastilla_imagen->url}' alt='{$banner->title}' /></a>".
	"<p class='description'>{$banner->pastilla_descripcion}</p>".
   "</div>";
 print($banner->pastilla_imagen->name);
}

What can I do?

Link to comment
Share on other sites

When using image_field->url, I get "/site/assets/files/1110/" without the image name or the extension.

I agree with Pete. This sounds like you are accessing type 'Pageimages' rather than type 'Pageimage' (singular). Since you are bootstrapping ProcessWire, output formatting is going to be off. One of the side effects of that is that all image fields are treated the same (as arrays of images) rather than single images. Since you are using ProcessWire's data for output in this case, I suggest adding a single call to this before you retrieve any pages from PW:

wire('pages')->setOutputFormatting(true); 

That will turn it on for all anything returned from the $pages API var.

Or you can do it for just one page, like your banner $page:

$banner->setOutputFormatting(true); 

Turning outputFormatting on will ensure you get the correct behavior for output. And if your image field is set to hold max 1 image, then it'll also ensure you only get that when accessing $banner->pastilla_imagen.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...