wordpress image path, how to edit? - php

i have a small problem in figuring out a path to a image var.
here is what happens. this is the image tag:
<img src="http://www.xxx.info/wp-content/uploads/http://ecx.images-amazon.com/images/I/51AV8B7CT2L._SL160_.jpg" class="attachment-135x135 wp-post-image" alt="Clean & Sober" title="Clean & Sober">
and this is how i would like it to be, without the http://www.xxx.info/wp-content/uploads/:
<img src="http://ecx.images-amazon.com/images/I/51AV8B7CT2L._SL160_.jpg" class="attachment-135x135 wp-post-image" alt="Clean & Sober" title="Clean & Sober">
here is the wordpress code:
<?php $thumb = '';
$width = 135;
$height = 135;
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Entry');
$thumb = $thumbnail["thumb"]; ?>
<?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
more related functions:
/* this function prints thumbnail from Post Thumbnail or Custom field or First post image */
function print_thumbnail($thumbnail = '', $use_timthumb = true, $alttext = '', $width = 100, $height = 100, $class = '', $echoout = true, $forstyle = false, $resize = true, $post='') {
if ( $post == '' ) global $post;
$output = '';
$thumbnail_orig = $thumbnail;
$thumbnail = et_multisite_thumbnail($thumbnail);
$cropPosition = get_post_meta($post->ID, 'etcrop', true) ? get_post_meta($post->ID, 'etcrop', true) : '';
if ($cropPosition <> '') $cropPosition = '&a=' . $cropPosition;
if ($forstyle === false) {
if ($use_timthumb === false) {
$output = $thumbnail_orig;
} else {
$output = '<img src="'.get_bloginfo('template_directory').'/timthumb.php?src='.$thumbnail.'&h='. $height .'&w='. $width .'&zc=1&q=90'.$cropPosition.'"';
if ($class <> '') $output .= " class='$class' ";
$output .= " alt='$alttext' width='$width' height='$height' />";
if (!$resize) $output = $thumbnail;
}
} else {
$output = $thumbnail;
if ($use_timthumb === false) {
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $output, $matches);
$output = $matches[1][0];
} else {
$output = get_bloginfo('template_directory').'/timthumb.php?src='.$output.'&h='.$height.'&w='.$width.'&q=90&zc=1'.$cropPosition;
}
}
if ($echoout) echo $output;
else return $output;
}
this is what i get returned $output = $thumbnail_orig; in the image tag.
i know that there should be a var for http://www.xxx.info/wp-content/uploads/
and another one for http://ecx.images-amazon.com/images/I/51AV8B7CT2L._SL160_.jpg
i want to remove the the website path to uploads.
i cant seem to figure it out,
All help appreciated,
Thanks

I might be wrong or misunderstanding the question, but I thought setting the path in WP is as simple as editing it in the admin panel, under settings, no?
DC

Related

How to display Dynamic images from GD library to html img tag

Hello i have images gotten from the database, after applying image convolution to them i am trying to output the image into an html tag. how do i go around this, have been trying to rack my brain for 3 days now no result looked at all the post i could find still no way to go around this
$eyt = pathinfo($mname, PATHINFO_EXTENSION); //get image extension
if($eyt=='jpg' || $eyt=='jpeg' || $eyt=='JPG' || $eyt=='JPEG')
{
$sr=imagecreatefromjpeg('media/'.$mname.'');
}
if($eyt=='png' || $eyt=='PNG')
{
$sr=imagecreatefrompng('media/'.$mname.'');
}
if($eyt=='gif' || $eyt=='GIF')
{
$sr=imagecreatefromgif('media/'.$mname.'');
}
$matrix = array(
array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1),
);
$divisor = array_sum(array_map('array_sum', $matrix));
$offset = 0;
imageconvolution($sr, $matrix, $divisor, $offset);
//output what i tried took the output code from a stackoverflow post tried to edit it to my format but no result
if($eyt=='jpg' || $eyt=='jpeg' || $eyt=='JPG' || $eyt=='JPEG')
{
ob_start();
imagejpeg($sr);
$raw = ob_get_clean();
$er= 'data:image/jpg;base64,' . base64_encode( $raw ) . '';
}
if($eyt=='png' || $eyt=='PNG')
{
ob_start();
imagepng($sr);
$raw = ob_get_clean();
$er= 'data:image/png;base64,' . base64_encode( $raw ) . '';
}
if($eyt=='gif' || $eyt=='GIF')
{
ob_start();
imagegif($sr);
$raw = ob_get_clean();
$er= 'data:image/gif;base64,' . base64_encode( $raw ) . '';
}
<img class="img-responsive pad tyi abn" src="media/<?php echo $er ?>" >
When i check my source code this is what i get
<img src="media/data:image/jpg;base64," . base64_encode( $raw ) . " >

Wordpress is generating thumbnails at 400px x 300px how do I change this to 210px x 210px

I have a theme that displays posts as a thumbnail using the code below but it is cropping it at the wrong ratio (400px x 300px) and I can't figure out what's telling it to do that. How would I change the thumbnails to be generated at 210px x 210px?
<?php
foreach($all_photo_arr as $key => $portfolio_item)
{
$cur_post++;
$image_url = '';
if(has_post_thumbnail($portfolio_item->ID, 'portfolio2'))
{
$image_id = get_post_thumbnail_id($portfolio_item->ID);
$image_url = wp_get_attachment_image_src($image_id, 'portfolio2', true);
$full_image_url = wp_get_attachment_image_src($image_id, true);
}
$last_class = '';
$line_break = '';
if(($key+1) % 4 == 0)
{
$last_class = ' last';
$line_break = '<br class="clear"/>';
}
$portfolio_link_url = get_post_meta($portfolio_item->ID, 'portfolio_link_url', true);
if(empty($portfolio_link_url))
{
$permalink_url = get_permalink($portfolio_item->ID);
}
else
{
$permalink_url = $portfolio_link_url;
}
$portfolio_item_class = 'one_fourth';
if(($key+1) % 4 == 0)
{
$portfolio_item_class.= ' last';
}
$pp_portfolio_image_height = 210;
?>
You can change the thumbnail size in Settings > Media.
This might help:
http://wordpress.org/support/topic/change-featured-image-size-only-on-homepage

Convert Image To base64 while fetching them from other urls

I'm using a php to code to fetch images and data from the other urls
but need to convert images to base64 string..!!
the code is
<?php
function getMetaTitle($content){
$pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui";
if(preg_match($pattern, $content, $match))
return $match[1];
else
return false;
}
function fetch_record($path)
{
$file = fopen($path, "r");
if (!$file)
{
exit("Problem occured");
}
$data = '';
while (!feof($file))
{
$data .= fgets($file, 1024);
}
return $data;
}
$url = $_POST['url'];
$data = array();
// get url title
$content = #file_get_contents($url);
$data['title'] = getMetaTitle($content);
// get url description from meta tag
$tags = #get_meta_tags($url);
$data['description'] = $tags['description'];
$string = fetch_record($url);
// fetch images
$image_regex = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex, $content, $img, PREG_PATTERN_ORDER);
$images_array = $img[1];
$k=1;
for ($i=0;$i<=sizeof($images_array);$i++)
{
if(#$images_array[$i])
{
if(#getimagesize(#$images_array[$i]))
{
list($width, $height, $type, $attr) = getimagesize(#$images_array[$i]);
if($width > 50 && $height > 50 ){
$data['images'] = "<img src='".#$images_array[$i]."' id='".$k."' width='100%'>";
$k++;
}
}
}
}
$data['form'] = '<input type="hidden" name="images" value="'.$data['images'].'"/>
<input type="hidden" name="title" value="'.$data['title'].'"/>
<input type="hidden" name="description" value="'.$data['description'].'"/>';
$dom = new domDocument;
#$dom->loadHTML($content);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
foreach($images as $img)
{
$url = $img->getAttribute('src');
$alt = $img->getAttribute('alt');
$pos = strpos($url, 'http://');
if ($pos === false) {
// $data['images'] = '<img src="'.$_POST['url'].''.$url.'" title="'.$alt.'"/>';
} else {
// $data['images'] = '<img src="'.$url.'" title="'.$alt.'"/>';
}
}
echo json_encode($data);
?>
This code use images in there Standard extension on this line
$data['images'] = "<img src='".#$images_array[$i]."' id='".$k."' width='100%'>";
I want to convert them to base64 and them use
Once you have the url for the image you just need to grab it with curl and call base64_encode.
chunk_split just makes it perdy.
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
$ret_val = curl_exec($curl);
// TODO: error checking!!!
$b64_image_data = chunk_split(base64_encode($ret_val));
curl_close($curl);
Another option is to get PHP to filter the image's binary data into a Base64 encoded value with a stream conversion filter (docs).
$img_url = 'http://www.php.net/images/php.gif';
$b64_url = 'php://filter/read=convert.base64-encode/resource='.$img_url;
$b64_img = file_get_contents($b64_url);
echo $b64_img;

Extract links from a string and putting them to an array and then parse them

I have a little regex script in PHP that made clickable all my links from a string that looks like
function clickable_link($text)
{
$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);
$ret = ' ' . $text;
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?#\[\]+]*)#is", "\\1\\2", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?#\[\]+]*)#is", "\\1\\2", $ret);
$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)#([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1\\2#\\3", $ret);
return $ret;
}
and works fine, but i would like a small adjustment, like to check when its a YouTube link to not make him as
<a href=youtube>youtube</a>
but rather (if there is an youtube link) as
<iframe width="425" height="349" src="http://www.youtube.com/embed/youtube" frameborder="0" allowfullscreen></iframe>
and
<img src="link" />
if it is an image.
Any help would be appreciated.
I have wrote a little script for all of that, but its too SLOW!!!!!!!!!
<?php
function MakeContentInteractive($string)
{
$order = array("<br>", "<br/>", "<br />");
$replace = ' <br/> ';
$string = str_replace($order, $replace, $string);
$firstImageSetted = false;
$firstImage = "";
$allval = "";
$pieces = explode(" ", $string);
$regex = "^(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk|co|tk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$^"; // SCHEME
$i=0;
foreach($pieces as $val)
{
echo $val."<hr>";
$i++;
$url = $val;
$url = str_replace(" ", "+", $url);
$strlen = strlen($url);
$ext = substr($val,$strlen-4,$strlen);
$random = rand(1000000,9000000);
if(preg_match($regex, $url))
{
/*CHECK IF IS YOUTUBE*/
$pos = strpos($url,"youtube.com");
if ($pos !== false)
{
//retrive video from link
$videoLink = $val;
$videoLinkPharser = $videoLink;
$videoLinkPharser = substr($videoLinkPharser, 2, 42);
$vid = substr($videoLinkPharser, -11, 42);
//check if youtube link is valid
$youtubeId = $vid;
// Check if youtube video item exists by the existance of the the 200 response
$headers = get_headers('http://gdata.youtube.com/feeds/api/videos/' . $vid);
if (!strpos($headers[0], '200'))
{
$valid = 0;
}
else
{
$isYoutube = 1;
$valid = 1;
$code = '<div id="YoutubeLink"><iframe width="425" height="349" src="http://www.youtube.com/embed/'.$vid.'" frameborder="0" allowfullscreen></iframe></div>';
$allval = $allval.$code;
}
}
if(!$isYoutube == 1)
{
$url=trim($url);
/*CHECK IF IS PICTURE*/
$mime = getimagesize($url);
$mime = $mime['mime'];
if($mime == "image/gif" or $mime == "image/jpeg" or $mime == "image/png")
{
echo $url;
if(exif_imagetype($url) == IMAGETYPE_GIF and $ext == ".gif")
{
$isPicture = 1;
$filename =$random.basename($url);
$code = '<div id="CategoryPicture"><img src="'.$val.'" width="100" height="100" /><div>';
$allval = $allval.$code;
if($firstImageSetted == false)
{
$firstImage=$val;
$firstImageSetted = true;
}
}
if(exif_imagetype($url) == IMAGETYPE_JPEG and $ext == ".jpg")
{
$isPicture = 1;
$filename =$random.basename($url);
$code = '<div id="CategoryPicture"><img src="'.$val.'" width="100" height="100" /><div>';
$allval = $allval.$code;
if($firstImageSetted == false)
{
$firstImage=$val;
$firstImageSetted = true;
echo "JPG!";
}
}
if(exif_imagetype($url) == IMAGETYPE_PNG and $ext == ".png")
{
$isPicture = 1;
$filename =$random.basename($url);
$code = '<div id="CategoryPicture"><img src="'.$val.'" width="100" height="100" /><div>';
$allval = $allval.$code;
if($firstImageSetted == false)
{
$firstImage=$val;
$firstImageSetted = true;
}
}
}
}
/*IF not YOUTUBE or PICTURE then it's a link*/
if(!$isYoutube == 1 and !$isPicture == 1)
{
$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $url);
$ret = ' ' . $text;
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?#\[\]+]*)#is", "\\1\\2", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?#\[\]+]*)#is", "\\1\\2", $ret);
$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)#([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1\\2#\\3", $ret);
$code = ''.$url.'';
$allval = $allval.$ret;
}
$isYoutube = 0;
$isPicture = 0;
}
else
{
$allval = $allval.$val;
}
}
echo "and the first image is: ".$firstImage."<br/>";
return $allval;
}
?>
And the slow part is when checks the image with exif and getimage size ( 3 seconds per picture !!!) How can i solve that???
Maybe add
$ret = preg_replace("#http\://www.youtube.com/watch\?v=([a-z0-9-_])+(&feature=[a-z_]*)*#is",
'<iframe width="425" height="349" src="http://www.youtube.com/embed/\1" frameborder="0" allowfullscreen></iframe>');
for Youtube and
$ret = preg_replace("#https?\://[a-z0-9\-.]*/[^\s]+((\.jpg)|(\.jpeg)|(\.png)|(\.gif)|(\.bmp))#is",
'<img src="\0" />');
for images. But you better do all replacements with one call to avoid replacing already replaced links. preg_replace can take arrays as pattern and replacement args.
But you can't be sure if the URL links to an image until you get a server response from it. You can only suggest that if a link ends with ".jpg", ".jpeg", ".gif", ".bmp" then it may be image. But it can be something like "http://www.google.com/search?q=trollface.jpg" which ends with ".jpg" but is not an image. You can use CURL to check such links but this may be a productivity issue.
EDIT: OK, there's a problem with your updated code. The script is so slow because you send requests to other servers and the main part of delay is awating for their response. First, I think it's not necessary to check if video is present on youtube when you have link like http://www.youtube.com/watch?v=blahblah&feature=blah . You just can take the code blahblah and embed it. If there's no such video, then youtube will tell us about it, this is the problem of the person who posted that link. I think the preg_replace which I wrote is enough.
Second, you call image processing functions several times for the same URL. And each time the image must be downloaded from other server. You should request the server only once -- to download the image (or whatever will be the response) to temporary file and then pass it's path instead of URL to image functions.

How to code php function or set parameters to return NO height values for images?

This question comes from this How to proportionally size images to fit dimensions of 200px x 150px thumbnail in css?, but since I feel it`s not a CSS related question anymore I thought I would create a new question. I am trying to proportionally fit images into thumbnails here http://giantmango.com/contest. I tried setting the css img tag to the below, but all images have the size of 200px x 200px. There is not another css line that has 200px as a height. I am suspecting it is something else.
img {
max-height: 150px;
max-width: 200px;
}
This function is called to return the images and thinking it might be this.
<?php
$content = get_the_content('Concept');
$content = apply_filters('the_content', $content);
list($col_class, $grid_img) = adjust_grid_image(
$content,
$col_w,
$gap_w,
$max_col,
$flg_img_forcelink,
$flg_obj_fit
);
?>
<div <?php post_class('grid-item ' . $col_class); ?> id="post-<?php the_ID(); ?>">
<?php if ($grid_img) echo '<div class="grid-image">' . $grid_img . '</div>'; ?>
These are the parameters that I have it set to.
$col_w = 200; // width of grid column
$gap_w = 7; // padding + margin-right (15+15+5)
$max_col = 5; // max column size (style div.x1 ~ xN)
$flg_img_forcelink = true; // add/overwrite a link which links to a single post (permalink).
$flg_img_extract = true; // in single post page, extract thumbnail link to an original image.
$flg_obj_fit = 'large-fit'; // none | small-fit | large-fit ... how to fit size of object tag.
This is the functions.php file that runs adjust_grid_image, but I am unsure of what it is doing. Is this setting my images to 200px x 200px? If so, what parameter must I change in order for it to not hardcode 200px as the height of images so I can set it in the css?
/*
* return class name and image tag (resized w/h attributes) to fit a grid.
*/
function adjust_grid_image($content, $col_w, $gap_w, $max_col, $flg_img_forcelink, $flg_obj_fit) {
global $post;
$col_class_base = 'x';
$col_class = $col_class_base . '1'; // default column-width class
$arr_w = array();
for ($i=0; $i<$max_col; $i++) {
$arr_w[] = ($col_w * ($i+1)) + ($gap_w * $i);
}
$grid_img = '';
$w = $h = 0;
$matches1 = $matches2 = $matches3 = array();
// search *first* img/object tag
preg_match('/<(img|object)(?:[^>]+?)>/', $content, $matches1);
if ($matches1[1] == 'img') {
preg_match('/<img(?:.+?)src="(.+?)"(?:[^>]+?)>/', $content, $matches2);
$img_url = ($matches2[1]) ? $matches2[1] : '';
if ($img_url) {
// first, try to get attributes
$matches_w = $matches_h = array();
preg_match('/width="([0-9]+)"/', $matches2[0], $matches_w);
preg_match('/height="([0-9]+)"/', $matches2[0], $matches_h);
if ($matches_w[1] and $matches_h[1]) {
$w = $matches_w[1];
$h = $matches_h[1];
}
else {
// ... or get original size info.
$upload_path = trim( get_option('upload_path') );
$mark = substr(strrchr($upload_path, "/"), 1); // default mark is 'uploads'
preg_match("#$mark(/.+)$#", $img_url, $split_url);
// split($mark, $img_url)
if ($split_url[1] != null) {
$img_path = $upload_path . $split_url[1];
list($w, $h) = #getimagesize($img_path);
}
}
}
for ($i=0; $i<$max_col; $i++) { // set new width and col_class
if ( ($i >= $max_col - 1) or ($w < $arr_w[$i+1]) ) {
$nw = $arr_w[$i];
$col_class = $col_class_base . ($i+1);
break;
}
}
$nh = (!$w or !$h) ? $nw : intval( ($h * $nw) / $w ); // set new height
$grid_img = $matches2[0];
// add width/height properties if nothing
$flg_no_w = (strpos($grid_img_edit, 'width=') === false);
$flg_no_h = (strpos($grid_img_edit, 'height=') === false);
if ($flg_no_w or $flg_no_h) {
$grid_img_close = (substr($grid_img, -2) == '/>') ? '/>' : '>';
$grid_img_edit = substr( $grid_img, 0, -(strlen($grid_img_close)) );
$grid_img_edit .= ($flg_no_w) ? ' width="0"' : '';
$grid_img_edit .= ($flg_no_h) ? ' height="0"' : '';
$grid_img = $grid_img_edit . $grid_img_close;
}
// replace new width/height properties
$grid_img = preg_replace('/width="(\d+)"/', 'width="'. $nw .'"', $grid_img);
$grid_img = preg_replace('/height="(\d+)"/', 'height="'. $nh .'"', $grid_img);
// check image link
//$chk_imglink = '/(<a(?:.+?)rel="(?:lightbox[^"]*?)"(?:[^>]*?)>)'. preg_quote($matches2[0], '/') .'/';
$chk_imglink = '/(<a(?:.+?)href="(?:.+?\.(?:jpe?g|png|gif))"(?:[^>]*?)>)'. preg_quote($matches2[0], '/') .'/';
if ($flg_img_forcelink) {
$grid_img = '' . $grid_img . '';
}
else if ( preg_match($chk_imglink, $content, $matches3) ) {
$grid_img = $matches3[1] . $grid_img . '</a>';
}
}
else if ($matches1[1] == 'object') {
preg_match('/<object(.+?)<\/object>/', $content, $matches2);
$matches_w = $matches_h = array();
preg_match('/width="([0-9]+)"/', $matches2[0], $matches_w);
preg_match('/height="([0-9]+)"/', $matches2[0], $matches_h);
if ($matches_w[1] and $matches_h[1]) {
$w = $matches_w[1];
$h = $matches_h[1];
}
else {
$flg_obj_fit = 'none';
}
//set col_class (and new width if in '*-fit' condition)
if ($flg_obj_fit == 'small-fit') {
for ($i=0; $i<$max_col; $i++) {
if ($i >= $max_col -1) {
$nw = $arr_w[$i];
$col_class = $col_class_base . ($i+1);
break;
}
else if ( $w < $arr_w[$i+1] ) {
$nw = $arr_w[$i];
$col_class = $col_class_base . ($i+1);
break;
}
}
}
else if ($flg_obj_fit == 'large-fit') {
for ($i=$max_col -1; $i>=0; $i--) {
if ( $w > $arr_w[$i] ) {
if ($i >= $max_col -1) {
$nw = $arr_w[$i];
$col_class = $col_class_base . ($i+1);
}
else {
$nw = $arr_w[$i+1];
$col_class = $col_class_base . ($i+2);
}
break;
}
if ($i == 0) {
$nw = $arr_w[$i];
$col_class = $col_class_base . ($i+1);
}
}
}
else {
for ($i=0; $i<$max_col; $i++) {
if ($i >= $max_col -1) {
$col_class = $col_class_base . ($i+1);
break;
}
else if ( $w < $arr_w[$i] ) {
$col_class = $col_class_base . ($i+1);
break;
}
}
}
$nh = (!$w or !$h) ? $nw : intval( ($h * $nw) / $w ); // set new height
$grid_img = $matches2[0];
if ($flg_obj_fit == 'small-fit' or $flg_obj_fit == 'large-fit') {
// replace new width/height properties
$grid_img = preg_replace('/width="(\d+)"/', 'width="'. $nw .'"', $grid_img);
$grid_img = preg_replace('/height="(\d+)"/', 'height="'. $nh .'"', $grid_img);
}
}
return array($col_class, $grid_img);
}
Thank you for looking at all of this.
This is some intermediate function. There will be probably some other part in the code which uses the return value from this function and actually outputs HTML. You should locate this part and comment the piece which output the width and height settings.
Pay attention, though, that if your images have variable proportions and you rescale them mantaining these proportions (via CSS), they will not fit in a grid anymore. They will look more scattered on the page than they are now.
EDIT Now that you have added more details, I think it should be enough to remove the section starting with the comment // add width/height properties if nothing

Categories